IIS7.5用web.config做域名的301跳转
学过一点seo知识的人都知道,一个空间如果绑定了两个或者多个域名(一般绑定两个域名的居多,比如www.it0527.com和it0527.com),为了不分散网站的权重,最好的方式就是做301重定向。现在使用IIS7或者IIS7.5的服务器或者虚拟主机越来越多,那么对于IIS7以上的虚拟主机如何做全站301跳转呢?IIS7以上的服务器支持通过web.config来修改配置。所以我们可以用web.config来做301重定向。
在修改web.config的过程中走了几段弯路,找了很多代码不能用,后来才发现症结所在。
现在先放出正确的代码,假如我从it0527.com跳转到www.it0527.com,那么web.config的代码可以这样写:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<defaultDocument>
<files>
<clear />
<add value="index.php" />
<add value="index.htm" />
<add value="index.html" />
</files>
</defaultDocument>
<rewrite>
<rules>
<rule name="WWW Redirect" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^it0527.com|www.sq01.cn$" /> </conditions>
<action type="Redirect" url="http://www.it0527.com/{R:0}"
redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
复制以上代码,写入web.config文件然后上传到根目录即可。记住把其中的域名换成你的域名。
通过web.config来设置301,操作起来的话,也就是说我们要手工修改web.config这个文件。该文件位于你的网站根目录。如果你没有,只要新建一个就行。文件的内容部分,就像下面这样。
关于代码中的一些指令,我说一下自己的理解。
! 叹号表示域名的开始;
^ 尖号表示非,即不是以 xxx 开头的;
$ 符号表示域名的结尾。
redirectType="Permanent",这表示永久重定向,也就是301重定向。
url="http://www.xxxx.com/{R:1}",这里的url自然是重定向后的url。
pattern="^xxxx.com|duodehen.info|duodehen.cn$",这部分是要被重定向的url,大家可以看到,我一共有3个域名。上面的设置全部不带www的,它们实现的功能当然是将不带www的重定向到带www的地址。如果你只有一个域名怎么做呢?
我举个例子,你可以这样写:pattern="^xxxx.com$"。
最后,你要到http://tool.chinaz.com/pagestatus/这里查询一下,你的301是否成功了。
例如我的
最后,对大家可能会产生的问题,根据我自己的经历来作一个回答。
1、百度对301多久会生效?
15天到30天
2、做了301以后百度多长时间响应?
2到3天的时间百度就知道你已经做了301重定向,这个你可以通过快照发现。
也就是说,尽管你的网址还是原来的,但你的快照内容却是重定向后的网址的。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="WWW Redirect" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^win7soft.com$" />
</conditions>
<action type="Redirect" url="http://www.win7soft.com/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
<location path="share/win7_ultimate_key">
<system.webServer>
<httpRedirect enabled="true" destination="http://item.taobao.com/item.htm?id=12730969071" exactDestination="true" childOnly="true" httpResponseStatus="Permanent" />
</system.webServer>
</location>
</configuration>
页:
[1]