Scot's Scripts
Scot's Scripts

Reputation: 302

How do I ignore the end of a url parameter string in Htaccess?

I have a bunch of ugly links coming in to a site like this:

https://www.somedomain.com/mm5/mch.mvc?Session_ID=5f59c6e0&Screen=PRODFB&Product_Code=pcode1&Category_Code=category_a&Store_Code=store1&fb=1

I'm trying to use htaccess to recreate the url like this:

https://www.somedomain.com/mm5/mch.mvc?Screen=PROD&Product_Code=pcode1

I've come up with this but of course it isn't working. I think I just need to be able to ignore the rest of the url parameter after the product_code param but not sure how to do it.

RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^Screen=PRODFB&Product_Code=([^/.]+)$ /mm5/mch.mvc?Screen=PROD&Product_Code=$1 [R=301,L,NE]

What am I missing? Thanks!

Upvotes: 1

Views: 51

Answers (2)

anubhava
anubhava

Reputation: 784898

You cannot match query string in RewriteRule directive.

As long as query parameters are same as what you have in question, you may use this rule in your site root .htaccess:

RewriteEngine On

RewriteCond %{THE_REQUEST} /(mm5/mch\.mvc)\?.*&Screen=PRODFB&(Product_Code=[^\s&]+) [NC]
RewriteRule ^ /%1?Screen=PROD&%2 [R=301,L,NE]

Or using %{QUERY_STRING}:

RewriteCond %{QUERY_STRING} (?:^|&)Screen=PRODFB&(Product_Code=[^&]+) [NC]
RewriteRule ^mm5/mch\.mvc/?$ %{REQUEST_URI}?Screen=PROD&%1 [R=301,L,NE]

Upvotes: 1

Scot's Scripts
Scot's Scripts

Reputation: 302

I was able to get it work like this:

RewriteCond %{QUERY_STRING} Screen=PRODFB&Product_Code=([^&]+)
RewriteRule ^(.*)$ /mm5/mch.mvc?Screen=PROD&Product_Code=%1& [R=301,L,NE]

Upvotes: 0

Related Questions