In this post I would like to share with you several useful rules for IIS URL rewrite module, which can increase rating of your site for search engines. Before to add these rules to the web.config we need to install URL rewrite extension on the web server, otherwise site won’t work.
The first rule is redirect with 301 (Permanent redirect) http status code from the url without www prefix to the url with www. Very often sites are available by both urls, e.g. http://www.example.com and http://example.com. Search engines may treat these sites as separate (most of search engines may handle this case, but I would not suggest experimenting) and as content on them will be the same, search rank may be decreased. In order to fix this problem we need to setup permanent redirect from one address to another. It is important to use permanent redirect (301), because in this case search engine will know that url from which redirect was made is not used anymore and can be deleted from search index. Another way to perform redirect is to use 302 (Moved temporarily), but in this case page won’t be deleted from index. In theory it also can impact search rating, but I didn’t find exact evidences so if you know that please share it in comments.
In order to add the redirection rule to the site running on IIS we will use IIS URL rewrite module. You can add rules by several ways:
- via UI – in IIS manager select site under the question and then select URL rewrite in the right panel (it will be added after installation of URL rewrite extension. Note that you need to restart IIS manager after installation. No need to make iisreset)
- directly to the web.config using any text editor, e.g. notepad.
UI is just convenient interface for adding rules to the web.config. Result in both cases will be the same: rules will be added to the web.config of your site. In this post I will show how to add rules to web.config directly.
Rules are added to the <system.webServer> section under <rewrite>/<rules>. Redirection rule from no www to www will look like this:
1: <rule name="redirect_from_nowwww_to_www" enabled="true" stopProcessing="true">
2: <match url=".*" />
3: <conditions>
4: <add input="{HTTP_HOST}" pattern="^example\.com$" />
5: </conditions>
6: <action type="Redirect" url="http://www.example.com/{R:0}"
7: appendQueryString="true" redirectType="Permanent" />
8: </rule>
Note that above I separated <action> to 2 lines (lines 6 and 7) in order to fit the article width, but in real example it should be single line. This rules tells to rewrite module that it should be applied to all urls (line 2) which have host “example.com” (line 4) and response should be redirected with 301 status code to “http://www.example.com” (lines 6-7). Here for matching urls we used regular expressions. {R:0} means back reference in term of regexp, which in this example has url part after the host. Plus we need to add possible query string in order to keep existing functionality working: see appendQueryString="true" attribute.
After that you can run fiddler and check that when you enter http://example.com in the browser, you will be redirected to http://www.example.com with 301 status. The rule will be also applied to all pages on the sites, including images and css.
Another useful rule is redirect from addresses without trailing slash to the addresses with slash: http://example.com/about –> http://example.com/about/. The reason here is the same: search engines may treat these url as different urls and if they will have the same content (in most cases they will), search rank can suffer. In order to add these redirection you can use the following url:
1: <rule name="add_trailing_slash" stopProcessing="true">
2: <match url="(.*[^/])$" />
3: <conditions>
4: <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
5: <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
6: </conditions>
7: <action type="Redirect" redirectType="Permanent" url="{R:1}/" />
8: </rule>
Here we used several conditions which mean that if physical file or folder are requested (matchType="IsFile" and matchType="IsDirectory"), the rule should not be applied (negate="true"). With this url you may have problems, e.g. if you use some CMS which allows to edit content of commonly used in SEO files robots.txt and sitemap.xml dynamically (i.e. if content is stored in the content database and routings are configured dynamically: http://example.com/robots.txt or http://example.com/sitemap.xml). In this case IIS won’t treat them as files and will add trailing slash: http://example.com/robots.txt/. Also it may cause problems on login view or administrative views if you use ASP.Net MVC (some actions won’t work). Solution is do disable this rule for these views. In order to avoid mentioned problems we need to add several additional conditions:
1: <add input="{REQUEST_FILENAME}" matchType="Pattern" negate="true"
2: pattern="robots\.txt" />
3: <add input="{REQUEST_FILENAME}" matchType="Pattern" negate="true"
4: pattern="sitemap\.xml" />
5: <add input="{REQUEST_URI}" matchType="Pattern" negate="true" pattern="^/admin.*" />
6: <add input="{REQUEST_URI}" matchType="Pattern" negate="true" pattern="^/users/.*" />
7: <add input="{REQUEST_URI}" matchType="Pattern" negate="true"
8: pattern="^/packaging/.*" />
This example contains conditions for administrative views for Orchard CMS. Here I excluded internal urls “/admin”, “/users”, “/packaging” from the rule (these urls are used in Orchard).
Note that also I used lowercase rule names with underscores, you may use more user friendly names for your rules. I used to this syntax :) That’s all what I wanted to write about in this article. Hope that it will be useful for you.
Don't you think "www." is anachronism? Would not it be better to redirect "www.consoso.com" to "contoso.com" instead?
ReplyDeleteI think it is more historical preference, then technical :)
ReplyDelete