Apache .htaccess 301永久重定向方法

所属分类:网站建设 2010-1-27,16:16 1,139 views

Apache .htaccess 301永久重定向方法的前提是服务器空间必须支持Apache的Rewrite功能。

一:
用301将不带WWW的域名重定向,如:wxfy.net 301转向到 www.wxfy.net

rewriteEngine on
rewriteCond %{http_host} ^wxfy.net[NC]
rewriteRule ^(.*)$ http://www.wxfy.net/$1 [R=301,L]

二:
301重定向还可以将绑定的其他域名重定向到主域名,例如将 wxfy.net ,www.wxfy.net 301转向到 anotherdomain.com

RewriteEngine on
RewriteCond %{HTTP_HOST} ^wxfy.net$ [OR]

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

三:
301还可以将index.html、index.php等文件进行重定向
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://wxfy.net/ [R=301,L]

以上方法是从网上搜集来的,因为有个网站需要换域名,所以要进行301永久重定向。 .htaccess代码如下:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^taiguorenyao.cn$ [OR]

RewriteCond %{HTTP_HOST} ^www.taiguorenyao.cn$

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

RewriteEngine on
RewriteCond %{HTTP_HOST} ^taiguorenyao.cn$ [OR]
RewriteCond %{HTTP_HOST} ^www.taiguorenyao.cn$
RewriteRule ^(.*)$ http://renyao.iyouwu.com/$1 [R=301,L]

您可以尝试一下打开原来的网址试试。

2 个评论 for "Apache .htaccess 301永久重定向方法"

 

  1. [...] 网站更换域名,必定要进行301永久重定向,以保证网站的正常运行。前面找了一篇Apache .htaccess 301永久重定向方法,昨天将人妖网做了301永久重定向,将域名http://taiguorenyao.cn换成http://renyao.iyouwu.com(其实最好换的新域名为一级域名,为什么后面会解释)。 [...]

  2. 子目录绑定方面好像不起作用

Leave a Reply