为什么要对顶级域名做301重定向?
这样做有利于SEO,原因是我们常常会碰到这样的情况,其他网站链接你的站点时,可能会用下面的链接:
www.maixj.net
maixj.net
www.maixj.net/index.php
maixj.net/index.php
而你的网站最好只能有一个主入口:www.maixj.net,需要将其它样式的链接都重定向到主入口,汇聚网站权重。
如何实现?
wordpress网站不需要做顶级域名301重定向。wordpress网站在配置了固定链接之后,会自动在网站根目录生成.htaccess,这个文件已经包含了301重定向的功能。
Apache服务器可通过修改配置来实现301重定向
1, 修改httpd.conf文件,确保ReWrite模块开启,并在相应目录支持ReWrite功能:
#将下面这一行的#号去掉
LoadModule rewrite_module modules/mod_rewrite.so
#网站目录下的Allowoverride设置
DocumentRoot "C:/maixj.net/..../site"
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options FollowSymLinks Includes ExecCGI
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride all
2, 设置.htaccess文件(一般虚拟主机都支持自己设置.htaccess文件)
使访问maixj.com的时候就会自动转到www.maixj.com
在网站根目录的.htaccess文件里写上以下代码即可:
RewriteEngine on
RewriteCond %{http_host} ^yourdomain.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]
这段代码的意识就是通正则表达式,将直接访问顶级域名的请求,全部转向www二级域名。
注意:URL标准化的301重定向(以上代码)需要写在其他URL-rewrite代码之前。
其它情况的方案
如果不能修改.htaccess文件,或者使用IIS系统,请同学们去搜索其它解决方案。
可参考:http://www.chinaz.com/web/2011/0125/156563.shtml