IIS Rewrite Module and ASP.NET MVC3

This morning I found out that the IIS Rewrite module causes a problem with MVC routing when trying to POST back to the server. I added a rule to the web.config to enforce all lowercase URLs as follows:

image

As you can see this is the rule that comes out of the box with the module. There doesn’t seem to be any notes included with this particular rule. This adds the following to the web.config

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true"></modules>
    <handlers>
    <remove name="UrlRoutingHandler" />
    </handlers>
    <rewrite>
        <rules>
            <rule name="LowerCaseRule1" stopProcessing="true">
                <match url="[A-Z]" ignoreCase="false" />
                <action type="Redirect" url="{ToLower:{URL}}" />
             </rule>
         </rules>
    </rewrite>
</system.webServer>

This works as expected as it changes http://localhost/SiteName/Account/Register  to http://localhost/sitename/account/register. We have decided to keep all URL’s lowercase and enforce the rule so Google cannot see duplicate URLs due to casing. When the user is presented with the register view, a form on that page has a POST action. When that POST action posts to a non-lowercase URL then the Rewrite module weighs in a redirects to the lowercase URL and presents the user with the view again and not with the results of the POST.

I mentioned this on twitter and Phil Winstanley, of Microsoft, kindly offered to enquire about this on the internal distribution lists to see what that brought back. The responses helped me understand what actually happen. they also pointed out that the behaviour, although it isn’t strictly correct, is kind of expected. According the HTTP spec

“If the 301 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request
unless it can be confirmed by the user, since this might change the conditions under which the request was issued.“ http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.2

It was also pointed out to me that as Login and Register are not really sites that are promoted for SEO purposes then we can exclude these routes from the redirect rule. Overall I have seen that I have not got a flawless plan for enforcing lowercase URLs. If I want lowercase URLs then I should really enforce them myself in my code. The one thing I’m slightly confused about is that there are no notes that come with the IIS Rewrite module that tell me these types of things. Fair enough I can get them from HTTP spec, but I really didn’t realise that this would be an issue.

If possible, I’d love people to see that the Rewrite module documentation may be lacking in specific areas. Hopefully this will improve. I’d like to thank Phil and all the guys at Microsoft who helped with responses in this query.

Comments (3) -

Piers Karsenbarg
Piers Karsenbarg
4/7/2011 10:42:26 AM #

The one problem that we had (and this might be us doing it wrong) is that if you make any changes to the url-rewrite module, it changes the web.config file which then restarts the application. If there's another way, then awesome, but this way isn't going to be good for us.

stack72
stack72
4/7/2011 4:59:18 PM #

Yeah thats the case - the url rewriter actually writes to the web.config - so a change to that will cause a web app restart Frown

Dan
Dan
10/21/2011 9:50:14 PM #

It seems the url rewrite module and making lowercase urls is meant to be good for SEO but I ran it on my site and google web master tools did not like it at all, because it creates a 301 which is the best way to redirect but in my case I had a lot of redirects due to the rewrite module which web master tools complained about.

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

About Me

 

Web Developer. My most used framework is C# with MVC but use Webforms on occasion. Im an advocate of clean, maintainable code and am very passionate for what I do. Absolutely obsessed with Continuous Integration and how it should be used in every day development scenarios. Trying to move towards a system of Continuous Deployment
Follow Me on Twitter

Jetbrains Academy Member

 

MVB Blogger

 

Friends Of Redgate

Month List