使用.htaccess重定向特定的URL

时间:2014-03-10 07:51:05

标签: php .htaccess redirect

如何像这样重定向网站:

www.old.com/test.php -> www.new.com/test.php

但请输入www.new.com/

的其他链接
www.new.com/test1.php -> www.old.com/test1.php
www.new.com/test2.php -> www.old.com/test2.php
www.new.com/test3.php -> www.old.com/test3.php

如果文件名不按顺序排列,如下所示:

www.new.com/home.php -> www.old.com/aiej.php
www.new.com/contactus.php -> www.old.com/contact.php
www.new.com/aboutus.php -> www.old.com/aboutus.php
www.new.com/msdv.php -> www.old.com/msdv.php

只有test.php会重定向到www.new.com/,其他人会重定向回www.old.com

我更喜欢使用.htaccess文件。有什么建议吗?

2 个答案:

答案 0 :(得分:1)

.htaccess

中试用此规则
RewriteEngine On
RewriteBase /

# Redirect test.php to new
RewriteCond %{HTTP_HOST} ^www\.old\.com$ [NC]
RewriteRule ^(test)\.php$ http://www.new.com/$1.php [R=301,L]

# Redirect test* to old
RewriteCond %{HTTP_HOST} ^www\.new\.com$ [NC]
RewriteRule ^test(.+)\.php$ http://www.old.com/test$1.php [R=301,L]

答案 1 :(得分:0)

RewriteCond %{HTTP_HOST} ^www.new.com$
RewriteRule ^test(1|2|3)\.php$ http://www.old.com%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTP_HOST} ^www.old.com$
RewriteRule ^test\.php$ http://www.new.com%{REQUEST_URI} [R=301,L]