修改.htaccess文件进行301重定向

RewriteCond %{HTTP_HOST} ^localhost$
RewriteRule ^all_article/$ http://localhost/all_article.html [R=301,L]php

 

RewriteCond %{HTTP_HOST} ^abc.com [NC]
RewriteRule ^(.*)$ http://www.abc.com/$1 [L,R=301]

 

最后再加一行
RewriteRule ^abc/(.*)$ http://www.abc.com/top/abc/ [L,R=301]

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^abc.com [NC]
RewriteRule ^(.*)$ http://www.abc.com/$1 [L,R=301]
RewriteRule ^abc/(.*)$ http://www.abc.com/top/abc/ [L,R=301]
若是是
http://www.abc.com/abc/list_18.html
要转向到
http://www.abc.com/top/abc/list_18.html
则换成
RewriteRule ^abc/(.*)$ http://www.abc.com/top/abc/$1 [L,R=301]
你原来的规则里已经加了L,意思是last

 

 

1、去除www,即域名由www.aaa.com简化到aaa.com,在.htaccess文件中添加以下规则语句:html

[cce_html]
RewriteEngine On
RewriteCond %{HTTP_HOST} !^aaa.com$ [NC]
RewriteRule ^(.*)$ http://aaa.com/$1 [L,R=301]

[/cce_html]

若是本来没有www,想加上www,那么在.htaccess文件中添加以下规则语句:[cce_html]post

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.aaa.com$ [NC]
RewriteRule ^(.*)$ http://www.aaa.com/$1 [L,R=301]

[/cce_html]

2、更换域名不用怕.在.htaccess文件中添加以下规则语句:
假设aaa.com为原域名,bbb.com为新域名.想把www.aaa.com变成www.bbb.com,那么在.htaccess文件中添加以下规则语句:
spa

[cce_html]
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.aaa.com$ [NC]
RewriteRule ^(.*)$ http://www.bbb.com/$1 [L,R=301]

[/cce_html]

想把aaa.com变成www.bbb.com,那么在.htaccess文件中添加以下规则语句:htm

[cce_html]
RewriteEngine On
RewriteCond %{HTTP_HOST} !^aaa.com$ [NC]
RewriteRule ^(.*)$ http://www.bbb.com/$1 [L,R=301]

[/cce_html]

想把aaa.com变成bbb.com,那么在.htaccess文件中添加以下规则语句:get

[cce_html]
RewriteEngine On
RewriteCond %{HTTP_HOST} !^aaa.com$ [NC]
RewriteRule ^(.*)$ http://bbb.com/$1 [L,R=301]

[/cce_html]

3、域名间文件夹重定向:
如你原来的域名为www.aaa.com,其中aaa/xxx.php 文件即http://www.aaa.com/aaa/xxx.php;在更换域名为www.bbb.com后想换个文件夹换个名字为bbb/yyy.php即http://www.bbb.com/bbb/yyy.php,那么在.htaccess文件中添加以下规则语句:域名

[cce_html]
RewriteCond %{HTTP_HOST} ^www.aaa.com$
RewriteRule ^aaa/xxx.php$ http://www.bbb.com/bbb/yyy.php [R=301,L]

[/cce_html]

4、延伸
修改.htaccess文件进行301重定向在Wordpress中的用法:
1.Wordpress更换域名时,你须要对域名进行重定向:
假设原域名为www.aaa.com 新域名为bbb.com,那么在.htaccess文件中添加以下规则语句:it

[cce_html]
RewriteEngine on
rewritecond %{http_host} ^www.aaa.com [NC]
rewriterule ^(.*)$ http://bbb.com/$1 [L,R=301]
rewritecond %{http_host} ^aaa.com [NC]
rewriterule ^(.*)$ http://bbb.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.bbb.com[NC]
RewriteRule ^(.*)$ http://bbb.com/$1 [L,R=301]

[/cce_html]

这样的结果就是当你输入 www.aaa.com、aaa.com、www.bbb.com,都会自动转为bbb.com。
2.更换域名后单纯使用域名重定向是不够的,尤为是你对Wordpress设置了固定连接的时候。
假设你的固定连接原格式为http://www.aaa.com/%year%/%post_id%/%postname%.html,你会发现添加以上规则之后,打开这个页面只会进入到bbb.com首页。那么咱们要添加下一段规则语句:io

[cce_html]RedirectMatch 301 /(.*)$  http://bbb.com/$1[/cce_html]

上面的语句为使用RedirectMatch实现文件匹配重定向。这样一来当你输入http://www.aaa.com/%year% /%post_id%/%postname%.html后,打开来地址就会自动转换为http://bbb.com/%year%/%post_id% /%postname%.html。
3.Wordpress 的分类页面地址里有一个 /category/ ,我感受这个不少余。下面这个规则语句就能去除它:ast

[cce_html]
RedirectMatch 301^/category/(.+)$ http://aaa.com/$1

[/cce_html]

或者:

[cce_html]
RewriteRule ^category/(.+)$ http://aaa.com/$1 [R=301,L]

[/cce_html]

4.自动检查英文地址的拼写错误并修复(这个我没有尝试过):

[cce_html]
<IfModule mod_speling.c>
CheckSpelling On
</IfModule>

[/cce_html]
相关文章
相关标签/搜索