使用Apache将旧域上的旧路径重定向到新域上的新路径

时间:2014-07-21 17:21:24

标签: apache .htaccess redirect

假设我有3个域名:

oldsite.com
anotheroldsite.com

newsite.com

我需要将特定网址从旧域重定向到新域上的特定路径。如:

oldsite.com/potato => www.newsite.com/how-about-a/potato
www.oldsite.com/potato => www.newsite.com/how-about-a/potato
和 anotheroldsite.com/products => www.newsite.com/sub-dir-before/products www.anotheroldsite.com/products => www.newsite.com/sub-dir-before/products

最后,我需要所有不匹配的请求来尝试www.newsite.com/$1

我花了最后一小时的大部分时间来寻找最好的方法来做到这一点,但我似乎无法找到任何查看旧域和具有完全不同路径的新域的示例。

重要提示:所有域名A记录都指向newsite.com的IP

1 个答案:

答案 0 :(得分:1)

您必须一次执行此一个网址,具体取决于您可以从旧网址映射到新网址的方式中获得哪些模式。

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(oldsite\.com|anotheroldsite\.com)$ [NC]
RewriteRule ^potato$ http://newsite.com/how-about-a/potato [L,R=301]

RewriteCond %{HTTP_HOST} ^(oldsite\.com|anotheroldsite\.com)$ [NC]
RewriteRule ^products$ http://newsite.com/sub-dir-before/products [L,R=301]


...

RewriteCond %{HTTP_HOST} ^(oldsite\.com|anotheroldsite\.com)$ [NC]
RewriteRule ^(.*)$ http://newsite.com/$1 [L,R=301]

相关问题