Apache .htaccess 301重定向规则

Apache实现301重定向最直接的方法是编辑.htaccess文件。

1. 重定向目录或文件 到其它地址

1
2
3
4
Redirect 301 /directory /newdirectory
Redirect 301 /directory http://otherdomain.com/other.php
Redirect 301 /oldfile.htm /newfile.htm
Redirect 301 /oldfile.htm http://otherdomain.com/newfile.htm

2. 重定向olddomain.com 到newdomain.com

1
2
3
4
RewriteEngine on
RewriteCond %{HTTP_HOST} ^olddomain.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301,NC]

3. 重定向olddomain.com目录或文件 到其它网址

1
2
3
RewriteEngine on
RewriteCond %{HTTP_HOST} ^olddomain.com$
RewriteRule ^old/file.php$ http://otherdomain.com/newfile.php [R=301,L]

4. 重定向www.yoursite.com 到yoursite.com

1
2
3
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.yoursite\.com [NC]
RewriteRule (.*) http://yoursite.com/$1 [R=301,L]

5. 重定向yoursite.com 到www.yoursite,com

1
2
3
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.yoursite\.com
RewriteRule (.*) http://www.yoursite.com/$1 [R=301,L]

Leave a Reply

Your email address will not be published.