Martin-QWS
Martin-QWS

Reputation: 23

Rewrite URL in .htaccess when 2 parameters are valid

I have some url like this:

index.php?cPath=23
index.php?cPath=23&sort=4a&language=En
index.php?currency=EUR&cPath=23&sort=4a&language=nl
index.php?currency=HUF&cPath=23&sort=4a&language=Hu

I want to redirect all of them to this link: shop/food. So I put this in my htaccess file:

# 301 --- /index.php?*cPath=23* => /shop/food
RewriteCond %{QUERY_STRING} (^|&)cPath=23($|&)
RewriteRule ^index\.php$ /shop/food? [L,R=301]

This is working great.

But... I also have some URL's like this:

product_info.php?cPath=23&products_id=1324&language=En&osCsid=204giavieen8nmfv95b0dfrgs5

With the above code this link is also redirected to /shop/food because cPath=23 is in the link.

I am looking now for a code which only redirect to /shop/food when index.php is in the link. In that way all these links will be redirected and the ones with product_info.php?cPath=23 will get a 404 not found error.

Or another way, links with product_info.php or products_id in it will NOT be redirect.

Upvotes: 1

Views: 216

Answers (1)

anubhava
anubhava

Reputation: 784928

You can use this rule:

RewriteCond %{QUERY_STRING} (^|&)cPath=23($|&)
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^index\.php$ /shop/food? [L,R=301]

Make sure this is placed right below RewriteEngine On.

On the use of %{THE_REQUEST}: THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules. Example value of this variable is GET /index.php?id=123 HTTP/1.1

Upvotes: 1

Related Questions