R – IIS7 Url Rewrite – Redirect from {any}.aspx to {any}

asp.net-mvciis-7redirecturl-rewriting

I am converting my website from Asp.Net Webforms to Asp.Net MVC. I want to redirect all my old .aspx files to drop the .aspx. I run IIS7 with the Url Rewrite module installed.

Example:

/about.aspx -> /about

The user will go to http://www.site.com/about.aspx and I want them redirected to http://www.site.com/about.

How do I do this using Url Rewrite? I don't want to have to do to each .aspx and put a meta-redirect in.

Best Answer

In your web.config file in system.webServer configuration section add:

<rewrite>
  <rules>
    <rule name="WebFromsToMVC" stopProcessing="true">
      <match url="^(.*?)\.aspx\?*?.*$" />
        <conditions>
          <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
          <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        </conditions>
      <action type="Rewrite" url="{R:1}" />
    </rule>
  </rules>
</rewrite>