Top 8 Useful IIS Rewrite Rules

11 augustus 2020 om 11:00 by ParTech Media - Post a comment

There are a plethora of routing options in ASP.NET available. However, still, there comes a situation when you have to tweak with a URL, and manipulation out of the code dynamics is useful for that situation. In this scenario, the best option ahead of you is to use the IIS Rewrite Module. When you transform numerous URL’s out of code, it will enable you to perform redirections for archives and can transfer content without intervention of codes.

Marketers can smoothly implement search engine optimization and manipulate quickly without changing code. URL rewrite module is an extension to IIS, which can be downloaded standalone on pre-installed Windows Azure Web Sites. Within IIS you can download and install it with the Microsoft Web Platform Installer

This post will walk you through some useful IIS rewrite rules that can come handy when you want to solve URL based problems for your website or web server.

1. Try tweaking with trailing slash

Numerous web applications are designed with “virtual URLs.” It is the URL that doesn’t directly take to the files or directory layout on the web server’s file system. For instance, ASP.NET MVC application is embedded with such a URL format that looks like - http://stackoverflow.com/questions/60857/modrewrite-equivalent-for-iis-7-0.

Now, if you try to search the same URL without the slash, the result will be the same, which is excellent for human visitors. But, for search engine crawlers or web analytics services, the case might be different. The different URLs for the same page may encourage crawlers to treat the same page differently; it can impact your page's ranking. This will split the web analytics for the page.

Thus, to fix this problem, you can rewrite IIS rules. Using a trailing slash in the URL or not, entirely depends upon your personal preferences. However, once you have made your decision, you can enforce the canonical URL by using these write rules.

To remove the trailing slash

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

To add a trailing slash

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

2. Try Manipulating ‘WWW’ Prefix

Similarly, to the trailing slash, you can add or remove the "www" prefix in your URL. By adding or removing "www" from the URL, you can make a significant change in your URL for a better ranking from the perspective of web crawlers and web analytics. Using with or without the www is your personal choice. Make sure you set it before the bots index your site so all content url are unique.

To add ‘www’ prefix use this rule

<rule name="non www to www" enabled="true">
    <match url="(.*)" />
    <conditions>
      <add input="{HTTP_HOST}" negate="true" pattern="^wwwt((.a-zA-Z0-9]+)$" />
    </conditions>
    <action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />      
 </rule>

To remove ‘www’ prefix use this code

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

3. Implement lower case URL rule

The same problem as the above two cases of www and trailing slash can arise when somebody links your website using different circumstances. Search bots will index the same pages as two separate pages. and two independent statistics will be created in the web analytics reports, which will impact the ranking. To solve the case problem, redirect visitors to the canonical URL even if they are using a non-canonical link. This code can help in implementing the solution:

<rule name="Convert to lower case" stopProcessing="true">
    <match url=".*[A-Z].*" ignoreCase="false" />
    <action type="Redirect" url="{ToLower:{R:0}}" redirectType="Permanent" />
</rule>

Source: https://ruslany.net/2009/04/10-url-rewriting-tips-and-tricks/

4. Redirect from Domain 1 to Domain 2

It is a beneficial IIS rewrite rule when you have changed the name of your site, or you want to direct traffic to your main website. If your new and old URL shares the same components, you just have to redirect your old domain to a new domain so that your visitors won’t get lost. To implement this redirect, you can use this rule:

<rule name="Domains 1 redirect" enabled="true">
    <match url="(.*)" ignoreCase="true" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="partechit.com" />
    </conditions>
    <action type="Redirect" url="http://www.partech.nl/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule> 

5. Redirect to HTTPS

When a non-secure HTTP connection accesses a website that requires SSL certification, IIS responds back with an HTTP 403 status code. There’s nothing to worry about if you want your visitors to type your website’s full URL starting from ‘http://..’. But, if you're going to make your website easily accessible for visitors without going through the 403 status code's trouble.

To direct your visitors to the URL's secure equivalent, you have rewrite URL module with the following rules:

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

Important Note – To implement this rule on the same website, you have to disable the “Require SSL” checkbox. However, if you don’t want to do that, you can set up two different websites in IIS – binding with one another and then inserting this rule to the web.config file of the site with https binding.

6. Particular subdomain redirection

When you want to remove an archived blog or site from your website, you have to apply this IIS rule to match the subdomain and then redirect to a new path. For example, you can send your blog.mysite.com/someentry/ to mysite.com/blog/entry/ anytime. It is primarily done for SEO purposes. For this website binding technique, you must insert the old blog domain hostname to complete the process.

<rule name="Subdomain redirection" stopProcessing="true" enabled="true">
    <match url="(.*)" ignoreCase="true" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="A(blogt)(.*)$" />
    </conditions>
    <action type="Redirect" url="http://www.mysite.com/" redirectType="Permanent" />
</rule> 

7. Handle HTTP 503 Status Code

HTTP 503 status code means that the server is right now unable to handle the request due to maintenance. It is a temporary situation, but when the search bot crawlers get this HTTP 503 server response from your website, it will not index this response but will come back later. However, if you are repairing a specific part of your website, there’s no need to send a crawler back. With the assistance of URL Rewrite Module, you can smoothly return 503 response when HTTP request for a specific URL pathway is made:

<rule name="Return 503" stopProcessing="true">
    <match url="^products/sale/.*" />
    <action type="CustomResponse" statusCode="503" subStatusCode="0" statusReason="Site is unavailable"
statusDescription="Site is down for maintenance" />
</rule>

8. Prevent other to hotlink your images

Image Hotlinking means taking images from other websites. With unauthorized image hotlinking, your bandwidth will increase. There are some other concerns with image hotlinking, such as copyright or using images with inappropriate context. By using the Rewrite Module, you can easily prevent image hotlinking with this rule:

<rule name="Prevent hotlinking of your images">
    <match url=".*\.(gif|jpg|png)$"/>
    <conditions>
        <add input="{HTTP_REFERER}" pattern="^$" negate="true" />
        <add input="{HTTP_REFERER}" pattern="^http://somesite\.com/.*$" negate="true" />
    </conditions>
    <action type="Rewrite" url="/images/do-not-use-my-images.gif" />
</rule>	

Using the URL Rewrite Module Rules can help in boosting your website ranking and performance. Above are some examples what you can do with simple rules without the use of code. Thus, follow the right IIS rewrite rules to welcome web crawlers and web analytics for better indexing. To read more visit the Microsoft docs.

Nieuwste