• Home /
  • Blog /
  • Examples of my most useful IIS rewrite rules
Examples Most Useful Iis Rewrite Rules

Examples of my most useful IIS rewrite rules

Over the years or work as a .NET software developer, I noticed that I use the same IIS rewrite rules over and over again on different projects to solve the same business problems. Below I share examples of most useful IIS rewrite rules which I use.

Keep all your rules in a separate file

Technically that is not a rewrite rule, but I would say it is one of the most important features for me because when you need to move rules between projects, it is very easy to copy a single file. Another benefit is keeping them separate allows easy version control management. When something does not work as it should it is much easy to look into a history of single rewrite rules file instead of going over every changeset of web.config.

To keep your rules in a separate file modify rules section as below, where configSource pointing to your rewrite rules file (you can name it differently if you want)

    <rewrite>     <rules configSource="rewriteRules.config" />   </rewrite> 

Your rewriteRules.config file will look like below, it does not need the schema declaration and starts directly from <rules> tag.

<rules>     <rule name="name of rule">     </rule>   </rules>   

Canonical hostname IIS rewrite rule

There are a lot of debates if your website should be prefixed with www or not, my opinion that it should not, so I usually remove that www prefix by redirecting users to non-www URL with the following rule. The rule is simple, it using regular expressions to verify if there is www in the URL and it extracts hostname without www.

  <rule name="Canonical Host Name (no WWW)" stopProcessing="true">     <match url="(.*)" ignoreCase="true" />     <conditions logicalGrouping="MatchAll">       <add input="{HTTP_HOST}" pattern="^www\.(.+)$" />     </conditions>     <action type="Redirect" url="http://{C:1}/{R:0}" appendQueryString="true" redirectType="Permanent" />   </rule> 

Redirect to HTTPS using IIS Rewrite rule

There are not too many websites left without SSL certificate and most of the existing sites already available on https. To avoid serving your content from two different URLs you need to redirect all requests to a secured version of your website using the following IIS rewrite rule.

  <rule name="Redirect to HTTPS" stopProcessing="true">     <match url="(.*)" />     <conditions>       <add input="{HTTPS}" pattern="^OFF$" ignoreCase="true"/>     </conditions>     <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />   </rule> 

Remove trailing URL slashes using IIS rewrite rule

This IIS rewrite rule serves to remove trailing slashes from your website URLs and avoid having the same page on two different URLs.

  <rule name="Remove trailing slash">     <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> 

IIS Rewrite rule for static 301 page redirect

When moving to different CMS, you often going to have different URL structure and in some cases, you need to redirect your old URLs to new to avoid a lot of 404 pages in search engines. The rule below doing a 301 (Permanent) redirect from your old URL to new URL. In my case, I keep a separate file with all redirects the file is defined in your web.config as below

   <rewrite>     <rewriteMaps configSource="rewritemaps.config" />     <rules configSource="rewriteRules.config" />   </rewrite> 

And the actual rule which you keep in rewriteRules.config file is below.

  <rule name="StaticRedirects from rewritemaps.config">     <match url=".*" />     <conditions>       <add input="{StaticRedirects:{REQUEST_URI}}" pattern="(.+)" />     </conditions>     <action type="Redirect" url="{C:1}" redirectType="Permanent" />   </rule> 

The rewritemaps.config file contains a one-to-one mapping between an old URL and the new URL and looks like below. For StaticRedirects you need to use the first section with the name “StaticRedirects,” that is the same name as you have in your condition. Another two parts in the rewriteMaps file will be explained below.

<rewriteMaps>     <rewriteMap name="StaticRedirects">       <add key="/clients" value="/clients/top-clients" />     </rewriteMap>         <rewriteMap name="StaticRewrites">       <add key="/robots.txt" value="/robotstxt" />     </rewriteMap>         <rewriteMap name="PageIsGone">       <add key="/powershell/create-a" value="/" />       <add key="/blog/tag/css" value="/" />     </rewriteMap>   </rewriteMaps>   

How to rewrite URL using IIS Rewrite rules

In some cases you need to rewrite one URL to another, for example, you have a robots.txt page which your CMS serves on /robotstxt URL, but actually, search engines expect it to be /robots.txt. That can be done using static rewrite rule below, where we use the same rewritemaps.config file, with a different section of that file used for rewrites.

  <rule name="StaticRewrites from rewritemaps.config">     <match url=".*" />     <conditions>       <add input="{StaticRewrites:{REQUEST_URI}}" pattern="(.+)" />     </conditions>     <action type="Rewrite" url="{C:1}" />   </rule> 
<rewriteMaps>     <rewriteMap name="StaticRedirects">       <add key="/clients" value="/clients/top-clients" />     </rewriteMap>         <rewriteMap name="StaticRewrites">       <add key="/robots.txt" value="/robotstxt" />     </rewriteMap>         <rewriteMap name="PageIsGone">       <add key="/powershell/create-a" value="/" />       <add key="/blog/tag/css" value="/" />     </rewriteMap>   </rewriteMaps>   

How to handle requests to pages which are no longer available on the website using IIS Rewrite rules

When you migrate to a different CMS, you may remove some pages from your website, because they no longer needed, but Google still has links to that pages and continuously trying to access them and gets back 404 error. To avoid that error and let search engines know that the page is gone you can use the following rule. It will let the search engine know that it should stop trying to access pages because it is not a temporary issue, but the pages removed permanently.

  <rule name="PageIsGone from rewritemaps.config" stopProcessing="true">     <match url="(.*)" />     <conditions>       <add input="{PageIsGone:{REQUEST_URI}}" pattern="(.+)" />     </conditions>     <action type="CustomResponse" statusCode="410" statusReason="Gone" statusDescription="Gone" />   </rule> 
<rewriteMaps>     <rewriteMap name="StaticRedirects">       <add key="/clients" value="/clients/top-clients" />     </rewriteMap>         <rewriteMap name="StaticRewrites">       <add key="/robots.txt" value="/robotstxt" />     </rewriteMap>         <rewriteMap name="PageIsGone">       <add key="/powershell/create-a" value="/" />       <add key="/blog/tag/css" value="/" />     </rewriteMap>   </rewriteMaps>   

Comments

Yaplex - Freelance developer - white logo on black background

Yaplex is an IT Consulting and .NET development company co-founded by freelance developer Alex Shapovalov who lives in Canada and provides software development services in Canada, USA and all over the world.

Contact Information

Toronto, Canada
Phone: +1 647 328 3809
Email: alex@yaplex.com