0
votes

I have tried various methods found on the web (including some SO answers) for getting URL rewrite in IIS 7 to work so that I can turn mysite.com/somepage.aspx into mysite.com/somepage, for example. The last thing I've tried is the video at this link: https://www.youtube.com/watch?v=bBNJE7XA1m0. After applying these changes in IIS, I now can request mysite.com/somepage and get to mysite.com/somepage.aspx with the .aspx removed in the address bar. Partial success.

When I try to directly request mysite.com/somepage.aspx, however, I get into a redirect loop. I am hoping there is some simple mistake in my settings. Here is the web.config section created by making changes in IIS:

<rewrite>
        <rules>
            <rule name="HideAspxExtension">
                <match url="^(.*)$" />
                <conditions logicalGrouping="MatchAny">
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    <add input="{REQUEST_FILENAME}.aspx" matchType="IsFile" />
                </conditions>
                <action type="Rewrite" url="{R:0}.aspx" />
            </rule>
            <rule name="RedirectingAspxExtension" stopProcessing="true">
                <match url="^(.*).aspx$" />
                <conditions logicalGrouping="MatchAny">
                    <add input="{URL}" pattern="^(.*).aspx$" />
                </conditions>
                <action type="Redirect" url="{R:1}" />
            </rule>
        </rules>
    </rewrite>

I have tried to apply this setting to multiple applications and I get the same results. What I do not have is another server to test on.

1

1 Answers

0
votes

These are rules I've used before:

<rule name="StripAspx">
    <match url="^(.+)\.aspx$" />
    <action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>
<!-- Rewrite the .aspx for internal processing-->
<rule name="RewriteASPX" stopProcessing="true">
    <match url="(.*)" />
    <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="{R:1}.aspx" />
</rule>

The main differences I can see are:

  • These have the removal of the .aspx first (in rewrite rules, order matters). This also means that in these rules the stopProcessing directive is on the rewrite to .aspx, not the redirect away from it.
  • These don't have <add input="{REQUEST_FILENAME}.aspx" matchType="IsFile" />