不带www域名重定向到带www域名

我们知道,网站域名通常包括不带www的域名和到带www的域名,很多时候,为了SEO优化的需要,我们要将不带www域名重定向到带www域名,下面就介绍一下具体的实现方法。

Apache

编辑网站根目录下的。htaccess 文件,在文件里增加如下内容:

RewriteEngine on

rewriteCond %{http_host} ^williamlong.info [NC]

rewriteRule ^(.*)$ http://www.williamlong.info/$1 [L,R=301]

IIS

在IIS 7.0以上的版本,有一个跟.htaccess非常类似的文件web.config,功能也非常强大,编辑根目录下的web.config文件,在文件里增加如下内容:

<?xml version=1.0 encoding=UTF-8?>

<configuration>

<system.webServer>

<rewrite>

<rules>

<rule name=WWW Redirect stopProcessing=true>

<match url=.* />

<conditions>

<add input={HTTP_HOST} pattern=^williamlong.info$ />

</conditions>

<action type=Redirect url=http://www.williamlong.info/{R:0} redirectType=Permanent />

</rule>

</rules>

</rewrite>

</system.webServer>

</configuration>

如果用户网站支持https的话,可以将重定向地址也修改为https,至于http如何重定向到https,请参考此文:IIS和APACHE实现HTTP重定向到HTTPS。

简简单单挺好的

联系我们 订单查询