重定向网址(如果它包含关键字),获取访问过的网址,删除关键字并在前面添加内容

时间:2014-10-23 17:34:29

标签: php apache .htaccess mod-rewrite redirect

如果标题有点令人困惑,我很抱歉,通过示例更容易解释。我想获取此网址以及包含find-in-set-any

的所有网址

http://www.example.com/orange/find-in-set-any/most-popular-products/find-in-set-any?limitstart=0

并将其重定向到:

http://www.example.com/our-products/orange/most-popular-products?limitstart=0

所以我删除了find-in-set-any的所有实例,但最后留下了?个内容

这也有点令人困惑,因为我们有语言子域,因此原始URL可能是

http://www.example.com/bg/orange/find-in-set-any/most-popular-products/find-in-set-any?limitstart=0

http://www.example.com/fr/orange/find-in-set-any/most-popular-products/find-in-set-any?limitstart=0

或58个不同语言子域中的任何一个。

我想保留现有的语言子域名。

如果我不清楚,我很抱歉。任何帮助将不胜感激。

关于已经尝试的规则我正在尝试这个atm,因为我认为我将它分成了几部分:

RewriteCond %{REQUEST_URI} find-in-set-any
RewriteRule ^/find-in-set-any(.*)$ /$1 [L,R=301]

但我必须完全错误,因为我无法让它产生任何影响。

修改

所以我最新的是:

RewriteEngine On
RewriteCond %{REQUEST_URI} find-in-set-any
RewriteRule (.*)find-in-set-any(.*) $1 $2 [N]
RewriteRule (.*)find-in-set-any/ $1 $2 [N]
RewriteRule (/)find-in-set-any(/) $1 $2 [N]

我很确定这不是最有效的方式。我剩下的就是在它之前添加我们的产品,这种情况很复杂,因为大约有50个语言子域fr/ bg/ de/等等。还有2或3个4位数的子域例如zh-CNzh-TW

1 个答案:

答案 0 :(得分:1)

您可以尝试以下规则:

RewriteEngine On
RewriteCond %{REQUEST_URI} find-in-set-any
RewriteCond %{REQUEST_URI} ^(?:(/[^/]+))?/([^/]+)/find-in-set-any/([^/]+)/find-in-set-any [NC]
RewriteRule ^ http://%{HTTP_HOST}%1/our-products/%2/%3 [L,R=301,QSA]

这只会重定向此网址:

http://www.example.com/fr/orange/find-in-set-any/most-popular-products/find-in-set-any?limitstart=0

http://example.com/fr/our-products/orange/most-popular-products?limitstart=0