Redirect and force URLs to lowercase and remove .aspx from filenames

For my day job I have a nice mix between PHP, WordPress and ASP .NET 4.0 coding with various projects. I’ve had to make some of our asp.net e-commerce projects more SEO friendly as I found out that people were sharing the same pages from our website with a mixture of upper-case urls and lower-case urls. Whilst I was at if I thought I would go a little step further and remove the default .aspx on the end to make them look nicer to the eye.

Also with ASP, webpages are treated slightly different than on Apache/Linux servers with PHP. About-us.aspx is different to about-us.aspx and we have to make sure there is a redirect or/and a canonical tag in place to let search engines know they are the same page. For time being I’m only going to show you to¬†redirect and force urls to lowercase rule. I’ve set this using the IIS 7.0 URL Rewrite tool and here is the output code:

Make sure the following rules are within the web.config file and between ‘<system.webServer>’ tag.

Redirect and force URls to lowercase using IIS, Web.config and the URL Rewrite function


<rewrite>
   <rules>
      <rule name="LowerCaseRule1" stopProcessing="true">
         <match url="[A-Z]" ignoreCase="false" />
         <action type="Redirect" url="{ToLower:{URL}}" />
      </rule>
   </rules>
</rewrite>

Now that we have the ‘camel case’ / uppercase urls redirecting to their lower-case counterparts we can move on to making the URLs of your website look pretty. We do this as it’s more user-friendly to the eye and Google (our friends) like this as your webpages will be easier to remember from a customer/visitor’s point of view.
The following code goes in the same place as a new rule within the <rules> tag. Just append the one above for lower-case URLs.

Remove .aspx from pages using URL Rewrite module in IIS for asp.net

This will redirect aspx pages to the pretty looking url (without the .aspx):


<rule name="Redirect to clean URL" stopProcessing="true">
   <match url="^([a-z0-9/]+).aspx$" ignoreCase="true"/>
   <action type="Redirect" url="{R:1}"/>
</rule>

Where you seel url=”{R:1}” you may want to replace this with url=”{ToLower:{R:1}}” so your urls follow suit with your first rule for lower case filenames.

There you have it, the ability to have pretty looking urls and forcing your asp.net project to use one url instead of a mix between uppercase and lowercase filenames.

All these rules can be achieved from using the URL Rewrite tool for IIS mentioned above, make sure this is installed to achieve the result and do even more like making query strings better looking and user-friendly.