HTAccess基于参数值重写,忽略其他参数

时间:2013-11-07 10:43:27

标签: php regex apache .htaccess mod-rewrite

另一个HTAccess重写问题,抱歉......

我正在尝试重写所有具有menuid = 28421,menuid = 27384和menuid = 73841的网址。问题是menuid是较长查询字符串的一部分,但查询字符串中并不存在所有参数。我只想根据menuid的值重写,而忽略其他参数。

http://www.domain.com/shop.php?menuid=28421&menuref=Ja&menutitle=New+Products&limit=5&page=8
http://www.domain.com/shop.php?menuid=27384&menuref=Ic&menutitle=Old+Products&page=3
http://www.domain.com/shop.php?menuid=73841&menuref=Hd&limit=10&page=14

重定向结果:

http://www.domain.com/new.html
http://www.domain.com/old.html
http://www.domain.com/sale.html

尝试以下方法,但没有骰子:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^menuid=28421$ [NC]
RewriteRule ^shop\.php$ http://www.domain.com/new.html? [R=301,NE,NC,L]

我根据我的产品类别的menuid自动生成wegpages:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^menuid=28421&page=1$ [NC]
RewriteRule ^shop\.php$ http://www.domain.com/new.html? [R=301,NE,NC,L]

@anubhava建议添加:

## 301 Redirects of Individual Menus, Ignoring the Limit Parameter etc...
RewriteEngine On
RewriteCond %{QUERY_STRING} (?:^|&)menuid=(?:28421|12356)(?:&|$) [NC]
RewriteRule ^shop\.php$ http://www.domain.com/new.html? [R=301,NE,NC,L]

感谢您的任何建议,

标记。

1 个答案:

答案 0 :(得分:1)

在这种情况下,您需要使用更好的正则表达式来匹配QUERY_STRING。

使用此规则:

RewriteEngine On

RewriteCond %{QUERY_STRING} (?:^|&)(menuid=[^&]+).+ [NC]
RewriteRule ^shop\.php$ %{REQUEST_URI}?%1 [R=302,NE,NC,L]

menuidid=28421可以出现在任何位置的QUERY_STRING中。