IIS URL Rewrite basic rewrite rules

URL Rewrite is built in IIS since IIS7, it helps to convert complex urls into easy to read urls, not only 
make it more readable for users but also search engine friendly. We will show you some basics about 
Rewrite rules.

Canonical Host Name
<rule name="Canonical Host Name" stopProcessing="true">
    <match url="(.*)" />
        <add input="{HTTP_HOST}" pattern="^$" negate="true" />
        <add input="{HTTP_HOST}" pattern="^example\.com$" />
        <add input="{HTTP_HOST}" pattern="^www\.example\.com$" negate="true" />
    <action type="Redirect" url="http://www.example.com/{R:1}" />
The rewrite rule above will redirect example.com to www.example.com  .

<match url="(.*)" /> this means to match all urls

<conditions> all urls must match the pattern inside the conditions tag.

<add input="{HTTP_HOST}" pattern="^$" negate="true" />
this means that http host must match "", with negate="true" means the url must not match

<add input="{HTTP_HOST}" pattern="^example\.com$" />
this means that http host must match example.com  .

Rewrite rule is performed when url match the rule pattern, in here we use Redirect action type, this way
rewrite moudle will send a redirect response to client, the browser will be redirected to www.example.com

<rule name="DifferentFolder" stopProcessing="true">
    <match url="^Examples/(.*).html$" />
    <action type="Rewrite" url="output/Examples/{R:1}.html" />
this rule will convert
into output/Examples/index.html or output/Examples/example-123.html

<rule name="Rewrite With id" stopProcessing="true">
    <match url="^(Article)-(\d+).html$" />
    <action type="Rewrite" url="/Article/index.aspx?aid={R:2}" />
this rule will convert

comments powered by Disqus