Skip to content

URL Rewrite to Remove .aspx in IIS 7 (& DNN)

by on May 4, 2011

This is a bit more work than in IIS 6 as you need to use the URL Rewriter module (there does not seem to be a way to implement the old isapi dll method properly).

The URL Rewriter module is available for download from Microsoft. You will need to install it on the server before implementing the following in the <system.webServer> section of the web.config file (this is located in the root of your DNN installation folder on the server).

On the plus side, the url rewriter also comes with some handy SEO features, such as removing a trailing slash and selecting a canonical domain (only a good idea if you have a single parent site which you want to navigate to via several domains).

The below code has a minor issue whereby any page names ending in “default.aspx” will keep the .aspx. The reason for this is a postback issue in DotNetNuke with login and search when navigating to the site without a page address (ie. mydomain.com). All other addresses postback just fine, but DNN loads “default.aspx” into the form at the root address.

I am hoping to come up with a more elegant solution in the future.

Add this to the <system.webServer> section:

<rewrite>
<rules>
<rule name=”RemoveASPX” enabled=”true” stopProcessing=”true”>
<match url=”(.*)\.aspx” />
<action type=”Redirect” url=”{R:1}” />
<conditions logicalGrouping=”MatchAll”>
<add input=”{URL}” pattern=”(.*)/default.aspx” negate=”true” />
<add input=”{URL}” pattern=”(.*)/admin/*” negate=”true” />
<add input=”{URL}” pattern=”(.*)/desktopmodules/*” negate=”true” />
</conditions>
</rule>
<rule name=”AddASPX” enabled=”true”>
<match url=”.*” negate=”false” />
<conditions>
<add input=”{REQUEST_FILENAME}” matchType=”IsFile” negate=”true” />
<add input=”{REQUEST_FILENAME}” matchType=”IsDirectory” negate=”true” />
<add input=”{URL}” pattern=”(.*)\.(.*)” negate=”true” />
</conditions>
<action type=”Rewrite” url=”{R:0}.aspx” />
</rule>
</rules>
</rewrite>

Advertisements
5 Comments
  1. Your solution is clean. As opposed to solutions from other people, yours include the code to redirect the .aspx page to the non-aspx URL, which avoids having a duplicate page indexed by Google (which I was looking for). Also, you exclude URLs including a dot. Great code 😉

    However, “logicalGrouping” and “enabled” tags aren’t necessary. I also removed your redirect conditions.

  2. Milan Thukral permalink

    Could You please help me in installing URL rewriter module on server….Its vry Urgent.
    Thanks In Advance.

    Regards
    Milan Thukral

  3. download the url rewrite module for IIS 7 from microsoft and install – it should now be available when you click on the website in the center pane of IIS 7 by clickin on Url Rewriter.

  4. Danish permalink

    This is awsome thanks for sharing it.

  5. Thanks for this. You might want to place the XML in a code block – copying it messes up the quotes in the XML!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: