Showing posts with label IIS7. Show all posts
Showing posts with label IIS7. Show all posts

Saturday, 13 March 2010

IIS7 rewrite rule for requests from the local network

First, if you don't know about the IIS7 rewrite module, read my earlier post on it.

You back? Good.

So today I was trying to debug my web app. My web server's internal ip is 192.168.1.80, but whenever I typed this into my browser, it would send me to http://www.192.168.1.80

I discovered that this was because of one of the rules I setup earlier in the IIS7 rewrite module:

<rule name="Non www to www redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.yoursite\.com$" ignoreCase="true" negate="true" />
</conditions>
<action type="Redirect" url="https://www.{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>

Can you see why? An IP address clearly doesn't match the pattern. To solve this problem, I just added a second condition to the rule:

<rule name="Non www to www redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.yoursite\.com$" ignoreCase="true" negate="true" />
<add input="{HTTP_HOST}" pattern="^192\.168\.*" ignoreCase="true" negate="true" />
</conditions>
<action type="Redirect" url="https://www.{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>

So what is this doing? Well, it's simply saying: "If the url contains the full site name, or the request comes from inside my network, ignore this rule". Or rather, it's APPLYING the rule to anything that DOESN'T match the conditions (that's what the "negate" attribute means).

That's why I love the IIS rewrite module. I love the way you can stack conditions like this. It's nice and understandable, and easy enough to change to do whatever you want.

Friday, 12 March 2010

Help! My HTTP Handler doesn't work in IIS7!

I faced this problem earlier today. Thankfully (as is usually the case) it was simple to resolve.

If you face this problem with an httphandler (usually .axd extension), make sure you have added a new "system.webServer" section to your web.config file. Yes, that's right, IIS7 doesn't look at the old httpHandlers and httpModules conig sections in system.web. You will need to copy your these sections into the new system.webServer section.

But wait, there's more! What caught me out today was the fact that IIS7 needs to see a name attribute in each "add" element. I am using a third party captcha on one of my sites - Lanap Botdetect. After puzzling over a 500.19 server error about incorrect configuration, I realised that IIS7 was expecting to see a name attribute, so I simply added name="LanapCaptcha" to the element (see below). Problem solved. I you have any specific questions related to httphandler problems, post them in the comments section below and I'll try and help you. If this post helped you, please Digg it using the Digg button at the top!

</system.webServer>

...

<handlers>

<remove name="WebServiceHandlerFactory-Integrated"/>

<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

<add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

<add name="LanapCaptcha" path="LanapCaptcha.aspx" verb="*" type="Lanap.BotDetect.CaptchaHandler, Lanap.BotDetect" />

<add name="ASBXHandler" verb="GET,HEAD,POST" path="*.asbx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

</handlers>

</system.webServer>

Monday, 8 March 2010

The power of the IIS7 url rewrite module

Having only recently got into IIS7 after buying a new web server, I am pleased to see that version 7 is somewhat following apache's lead by allowing modules to be plugged into the server. If you host anything beyond a very basic website in IIS 7, you will likely have the need to use http modules, very likely including some kind of url rewiting.

There can be many reasons to do url rewriting - Search Engine Optimization (friendly urls), Redirection to a secure site, or changing the request in some other way.

With IIS 6 you would either have to pay for a 3rd party ISAPI dll to do some of these, or spend time programming them yourself. But now the IIS 7 url rewrite module is here to come to the rescue.

All this involes is editing your web.config, using the new IIS7-only system.webServer configuration section (you can also do it through IIS Manager). Here are 2 examples.

The first rule I have created below redirects requests starting with "http" to the equivalent "https" url. You can do this for a specific area within your application too - just add another web.config within that subfolder with appropriate rules inside it's system.webServer -> rewrite section. You can also use tags if you want to keep all web.config stuff in one file. E.g.:

<location path="admin"><system.webserver><rewrite>... your rules...</rewrite></system.webserver></location>

The second rule redirects requests not containing www (e.g.: http://yoursite.com) to the url containing www. You can do the opposite quite easily too.

<system.webServer>
...
<rewrite>
<rules>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<!-- Require SSL must be OFF in the site settings -->
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}{REQUEST_URI}" />
</rule>
<rule name="Non www to www redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.yoursite\.com$" ignoreCase="true" negate="true" />
</conditions>
<action type="Redirect" url="https://www.{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>

See also the official documentation for the IIS7 url rewriting module.