Problem


When you change the domain name for your website, you are definitely going to solve the migration problem. You cannot just stop DNS on your old domain, because this will cause your indexed pages to be deleted by search engines. The correct way is to tell the search engine that you have a new domain name now, which is when the user accesses an old URL, redirect it to the new URL.

Take my blog as an example, my old domain is d******k.com, the new domain name is edi.wang. So, for requests to the old domain, such as:

http://d******k/post/2015/4/22/how-to-read-webconfig-appsettings-with-bigiblity

I need to redirect it to:

http://edi.wang/post/2015/4/22/how-to-read-webconfig-appsettings-with-bigiblity

Which is just replace the domain name and keep everything else in the URL.

Solution


To implement this feature, we need to install the URL Rewrite module on IIS: http://www.iis.net/downloads/microsoft/url-rewrite

For every website managed by IIS, it can assign URL rewrite or redirect rules individually. You can use both GUI and manually edit web.config to modify the rules.

My configuration for migrating the domain name would be:

<system.webServer>
	<rewrite>
		<rules>
			<rule name="CanonicalHostNameRule">
				<match url="^(.*)$" />
				<conditions>
					<add input="{HTTP_HOST}" pattern="^d*******k.com$" />
				</conditions>
				<action type="Redirect" url="http://edi.wang/{R:1}" />
			</rule>
		</rules>
	</rewrite>
</system.webServer>

The pattern is using a regular expression, to check if {HTTP_HOST} is equal to d*******k.com

the {R:1} in the action means whatever is in the URL behind the domain name itself.

We have two types: Redirect and RewriteRedirect will result in an HTTP 301, which will notify the search engine about the domain change, while Rewrite will just keep the HTTP status code, for example, you have a "/a/b/c.html" and rewrite into "/d.html", then Rewrite will just make it looks like you are accessing /d.html, and its content is output using c.html.

If you have other rules, mind the order, they are being processed from top to bottom.

You may refer to Microsoft documentation for more usage: http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference

Now, we can get a 301 redirection, the search engine will also update their index. After a couple of months, you can stop DNS on your old domain.