URL Rewriting – Top Rules

Here is the URL Rewrite Rule in the IIS web.config file Make sure you replace example.com with the name of your domain.

01. To always remove trailing slash from the URL:

<rule name=”Remove trailing slash” stopProcessing=”true”>
<match url=”(.*)/$” />
<conditions>
<add input=”{REQUEST_FILENAME}” matchType=”IsFile” negate=”true” />
<add input=”{REQUEST_FILENAME}” matchType=”IsDirectory” negate=”true” />
</conditions>
<action type=”Redirect” redirectType=”Permanent” url=”{R:1}” />
</rule>

02. To always add trailing slash to the URL:

<rule name=”Add trailing slash” stopProcessing=”true”>
<match url=”(.*[^/])$” />
<conditions>
<add input=”{REQUEST_FILENAME}” matchType=”IsFile” negate=”true” />
<add input=”{REQUEST_FILENAME}” matchType=”IsDirectory” negate=”true” />
</conditions>
<action type=”Redirect” redirectType=”Permanent” url=”{R:1}/” />
</rule>

03. Redirect Non-WWW to WWW:

<rulename=”Redirect to WWW”stopProcessing=”true”>
<matchurl=”.*”/>
<conditions>
<addinput=”{HTTP_HOST}”pattern=”^example.com”/>
</conditions>
<actiontype=”Redirect”url=”http://www.example.com/{R:0}”redirectType=”Permanent”/>
</rule>

 

This entry was posted in URLRewrite. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *