TroySteven
TroySteven

Reputation: 5157

Remove ASPX FILE Extention in Web.config But Negate Rewrite if URL Contains Period

All right, so by now most of us probably use the standard rule below for remove the aspx extention in your urls.

<rule name="Remove">
      <!--Removes the .aspx extension for all pages.-->
      <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>

However I would like to modify the rule to prevent the write rule from catching any url with a period in it.

That way if someone tries to type in http://www.domain.com/picture.jpg The rule doesn't catch it.

Luckily the IsFile and IsDirectory conditions prevent actual files from being hit by the rule but whenever I have a case where someone types in a file that doesn't exist on the server then the rule catches it and asp.net does something like this:

http://www.domain.com/404error.aspx?aspxerrorpath=/picture.jpg.aspx

Id like it to not pass through the rule when there is a file not found.

Basically I just need to be able to add a condition that negates whenever a period is found in the url after the domain name. I'm assuming some sort of REGEX will work. I can't seem to get it working right though.

Upvotes: 1

Views: 1974

Answers (1)

TroySteven
TroySteven

Reputation: 5157

I was able to come up with the following solution.

        <rule name="RewriteASPX" stopProcessing="true" enabled="true">
            <match url="(.*)" />
            <conditions logicalGrouping="MatchAll">
                <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:1}.aspx" />
        </rule>     

Upvotes: 0

Related Questions