.htaccess: Redirect some subpages to new subpage, all others to new main page

一曲冷凌霜 提交于 2020-01-06 05:45:07

问题


I have searched SO for answers but have not found any matching my criteria. I am moving a webshop to a new domain (it has 1000s of products). For the pages with some decent rankings, I want to redirect them to their new respective subpages. All other pages I want to redirect to the new main page. I am trying to accomplish this using .htaccess

shopold.com/cat/product1.html to shopnew.com/category/product1
shopold.com/cat/product2.html to shopnew.com/category/product2
All other pages from shopold.com to shopnew.com

What would be the easiest way to accomplish this?


回答1:


You may use these rules in your site root .htaccess:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(?:www\.)?shopold\.com$ [NC]

RewriteRule ^cat/product1\.html$ http://shopnew.com/category/product1 [L,NC,R=301]

RewriteRule ^cat/product2\.html$ http://shopnew.com/category/product2 [L,NC,R=301]

RewriteRule ^ http://shopnew.com/? [L,NC,R=301]

In general keep known-product rules at top and keep generic catch-all rule at the bottom.


Here is a non-regex way of comparing REQUEST_URI:

RewriteCond %{REQUEST_URI} =/cat/product1.html
RewriteRule ^ http://shopnew.com/category/product1 [L,NC,R=301]

Unfortunately it requires use of a RewriteCond since pattern in RewriteRule does require a regex.



来源:https://stackoverflow.com/questions/58483366/htaccess-redirect-some-subpages-to-new-subpage-all-others-to-new-main-page

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