.htaccess:将一些子页面重定向到新的子页面,将所有其他重定向到新的主页面

问题描述:

我已经搜索了 SO 的答案,但没有找到任何符合我的标准.我正在将一个网上商店转移到一个新域(它有 1000 种产品).对于排名不错的页面,我想将它们重定向到各自新的子页面.我想重定向到新主页的所有其他页面.我正在尝试使用 .htaccess

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?

您可以在站点根目录 .htaccess 中使用这些规则:

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.

这是一种比较 REQUEST_URI 的非正则表达式:

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]

不幸的是,它需要使用 RewriteCond,因为 RewriteRule 中的模式确实需要一个正则表达式.

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