Click here to Skip to main content
15,886,422 members
Articles / Web Development / IIS

IIS Rewrite Rules That You Can’t Live Without

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
4 Dec 2011CPOL 22.5K   5  
The most common Url rewrite rules for IIS 7 and above.

Listed here are the most common URL rewrite rules for IIS 7 and above.

Remove Trailing Slash Rule

XML
<rule name="Remove Trailing Slash Rule">
      <match url="(.*)/$" />
      <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
      </conditions>
      <action type="Redirect" redirectType="Permanent" url="{R:1}" />
</rule>

This rule will match all urls that end with a slash, remove the slash and return a redirect permanent response to the client.

Remove WWW.

XML
<rule name="Remove WWW" stopProcessing="true">
      <match url="^(.*)$" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
      </conditions>
      <action type="Redirect" 
        url="http://Example.com{PATH_INFO}" 
        redirectType="Permanent" />
</rule>

This rule will match all urls that start with ‘www’ and redirect to the naked domain url, while preserving any query strings in the url path. You must manually enter your domain name in the rule.

Prevent Image hotlinking

XML
<rule name="Prevent image hotlinking">
      <match url=".*\.(gif|jpg|png)$" />
      <conditions>
        <add input="{HTTP_REFERER}" 
          pattern="^$" negate="true" />
        <add input="{HTTP_REFERER}" 
          pattern="^http://Example\.com/.*$" negate="true" />
      </conditions>
      <action type="Rewrite" 
        url="/images/say_no_to_hotlinking.jpg" />
</rule>

This rule will match urls ending with .gif, .jpg, or .png, and check the referer header for your domain name, if the request is not coming from your domain, then it will return “say_no_to_hotlinking.jpg”. Be aware regarding Google bot and other search engines that you might want to allow them to hot-link to your images.

This article was originally posted at http://buaziz.com/2011/10/24/iis-url-rewrites-rules-list

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) KUFPEC
Kuwait Kuwait
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --