301重定向,分享301重定向的方法及规则

qinzhiqiang 09-29 10:57 590次浏览

编辑您网站根目录下面的.htaccess文件,将以下规则添加到文件末尾,如果没有该文件,请创建一个。

(说明: example.com 就是你要做的域名网址,自己替换就行。)

301重定向规则及实现方法

1.重定向example.com到www.example.com

这种重定向旨在使域名唯一,是网站SEO必须要做的,后面重定向www.example.com到example.com也是出于同样的原因,只是形式不同。

打开.htaccess文件,加入以下规则。(下面的规则是针对主域名的,子域名要修改)

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]

RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

2.重定向www.example.com到example.com

RewriteEngine On

RewriteCond %{HTTP_HOST} !^example.com$ [NC]

RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

3.重定向oldexample.com到www.newexample.com

RewriteEngine On

RewriteCond %{HTTP_HOST} !oldexample.com$ [NC]

RewriteRule ^(.*)$ http://www.newexample.com/$1 [L,R=301]

4.重定向oldexample.com to newexample.com

RewriteEngine On

RewriteBase /

RewriteCond %{HTTP_HOST} !oldexample.com$ [NC]

RewriteRule ^(.*)$ http://newexample.com/$1 [L,R=301]

5.重定向example.com/file/file.php 到 otherexample.com/otherfile/other.php

RewriteCond %{HTTP_HOST} ^www.example.com$

RewriteRule ^file/file.php$ http://www.otherexample.com/otherfile/other.php [R=301,L]