RSS

IIS Rewrite rules that you can’t live without

24 Oct

listed here are the most common Url rewrite rules for IIS 7 and above.

Remove Trailing Slash Rule

<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.

<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 manualy enter you domain name in the rule.

Prevent image hotlinking

<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.

Advertisements
 
Leave a comment

Posted by on 24, Oct 2011 in .NET

 

Tags:

add your comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: