using .htaccess redirect specific URL

删除回忆录丶 提交于 2020-01-06 14:01:10

问题


how to redirect the website like this :

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

but enter other links to 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

How about if the file names are not in sequence, like this:

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

only test.php will redirect to www.new.com/, others redirect back to www.old.com

I prefer to use .htaccess file. any suggestion?


回答1:


Try this rules in your .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]



回答2:


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]


来源:https://stackoverflow.com/questions/22294718/using-htaccess-redirect-specific-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!