301将.htaccess从旧域重定向到新域

时间:2013-09-18 13:06:44

标签: apache .htaccess mod-rewrite

有域old.test.ru和test.ru.如何将所有页面从old.test.ru/*重定向到test.ru / *?

我有这些.htaccess:

<IfModule mod_rewrite.c>
   Options +FollowSymLinks
   RewriteEngine On
   RewriteBase /

   # redirect from index.(php|asp|aspx|html|htm), main.(php|asp|aspx|html|htm), home.(php|asp|aspx|html|htm)
   RewriteRule ^(.*)index\.(php|asp|aspx|html|htm)$ $1 [R=301,L]
   RewriteRule ^(.*)main\.(php|asp|aspx|html|htm)$ $1 [R=301,L]
   RewriteRule ^(.*)home\.(php|asp|aspx|html|htm)$ $1 [R=301,L]

   # redirect from dev and www
   RewriteCond %{HTTP_HOST} ^old\.test\.ru$ [OR]
   RewriteCond %{HTTP_HOST} ^www\.test\.ru$
   RewriteRule ^(.*)$ http://test\.ru/$1 [R=301,NC]
</IfModule>

我认为最后一行

RewriteRule ^(.*)$ http://test\.ru/$1 [R=301,NC]

应该有效,但事实并非如此。

适用于:

old.test.ru/about.php -> test.ru/about.php
old.test.ru/company.php -> test.ru/company.php
old.test.ru/contact.php -> test.ru/contact.php

但不起作用:

old.test.ru/some/about.php -> test.ru/some/about.php
old.test.ru/some1/company.php -> test.ru/some1/company.php
old.test.ru/some2/contact.php -> test.ru/some2/contact.php
old.test.ru/some2/subsome/contact.php -> test.ru/some2/subsome/contact.php

我应该改变什么?

2 个答案:

答案 0 :(得分:1)

尝试用以下内容替换上一条规则:

 # redirect from dev and www
 RewriteCond %{HTTP_HOST} ^(www|old)\.test\.ru$ [NC]
 RewriteRule ^ http://test.ru%{REQUEST_URI} [R=301,L]

更新:从网址中删除index.php等:

 RewriteCond %{HTTP_HOST} ^(www|old)\.test\.ru$ [NC]
 RewriteRule ^(.*?)((?:index|default)\.(?:php|html?))?$ http://test.ru/$1 [R=301,L,NC]

答案 1 :(得分:0)

如果您想重定向所有请求,我会在old.test.ru上使用Redirect而不是使用mod_rewrite

old.test.ru使用的配置中:

Redirect permanent / http://test.ru/