Barton
Barton

Reputation: 63

HTAccess Rewrite based upon a Parameter Value, ignore other Parameters

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.

Upvotes: 1

Views: 1723

Answers (1)

anubhava
anubhava

Reputation: 785156

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.

Upvotes: 1

Related Questions