Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have an issue with IIS7 url rewrite module, when I add following rule I get an 404 error on all pages.

<rule name="Remove .aspx" stopProcessing="true">
<match url="(.+)\.aspx" />
<action type="Redirect" redirectType="Permanent" url="{R:1}" />

All I want to do to remove all files extensions. I get lost with this, maybe someone knows the solution?

Thanks in advance.

Regards, eimeim

share|improve this question
add comment

1 Answer

Removing .aspx from the URL by redirecting the user to the URL without .aspx is only one part of the solutions.

If you do that redirect, how will the server ever know what script (with .aspx extension) to execute if the URL doesn't contain the correct name (without .asp extension)? So you have to add a rule to fix that. The way to do that is to make a rule that matches any URL, but doesn't match any existing file or directory. If we encounter that, we can assume that it might be a rewritten link to an ASPX page so we rewrite the URL to add .aspx. If it isn't an ASPX page it will result in a 404 anyway.

To make it all work with your existing code you have to outbound rewrite the response of your pages to remove all existing references to .aspx pages to not include the extension. Otherwise you will get a lot of unnecessary redirects and form posts to aspx pages will not work anymore.

Last but not least you have to disable compression as the outbound rewriting does not work with compression turned on.

This all results in the following rewrite rules for you web.config:

<system.webServer>
    <rewrite>
        <rules>
            <rule name="Remove .aspx from URL" stopProcessing="true">
                <match url="(.*)\.aspx$" />
                <action type="Redirect" url="/{R:1}" />
            </rule>
            <rule name="Add .aspx for non-existing files or directories">
                <match url="(.*)" />
                <conditions>
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                </conditions>
                <action type="Rewrite" url="/{R:0}.aspx" />
            </rule>
        </rules>
        <outboundRules>
            <rule name="Remove .aspx from links in the response" preCondition="Only for HTML">
                <match filterByTags="A, Area, Base, Form, Frame, IFrame, Link, Script" pattern="(.*)\.aspx(\?.*)?$" />
                <action type="Rewrite" value="{R:1}{R:2}" />
            </rule>
            <preConditions>
                <preCondition name="Only for HTML">
                    <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
                </preCondition>
            </preConditions>
        </outboundRules>
    </rewrite>
    <urlCompression doStaticCompression="false" doDynamicCompression="false" />
</system.webServer>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.