HTAccess Rewrite based upon a Parameter Value, ignore other Parameters

為{幸葍}努か 提交于 2019-12-24 13:17:41

问题


another HTAccess rewrite issue, sorry....

I'm trying to rewrite all URL's that have menuid=28421, menuid=27384 and menuid=73841. The issue is that the menuid is part of a longer query string, but not all parameters are present in the query string. I want to rewrite only based on the value of menuid, and ignore the other parameters.

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

Result of redirection:

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

Tried this below, but no dice:

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

I automatically generate wegpages based on menuid for my product categories:

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

Suggested addition by @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]

Thank you for any advice,

Mark.


回答1:


You need to use a better regex for matching QUERY_STRING in this case.

Use this rule:

RewriteEngine On

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

menuidid=28421 can appear in QUERY_STRING at any position.



来源:https://stackoverflow.com/questions/19833834/htaccess-rewrite-based-upon-a-parameter-value-ignore-other-parameters

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