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 Rewrite. Redirect 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.
zimoli
好,请教个问题,你现在这个网站使用的.NET MVC吧,但是浏览器地址全是小写的,是在新建controller时直接用的小写还是其他方法?
陈志同
这样做好像很麻烦啊,IIS里有自带的HTTP重定向,在安装IIS时勾选上,然后在HTTP重定向里设置很方便。参考:http://blog.csdn.net/hk_yuting/article/details/6790660