Daew
Daew

Reputation: 419

.htaccess url rewrite inserts path to url

I am running a magento website. I noticed that some GET parameter foo=1 crashes one of the extensions. I would like to catch it and replace it with foo=0 to prevent foo from being 1. (I need foo for other reasons and can't remove it completely)

So I inserted this in my .htaccess file:

RewriteCond %{QUERY_STRING} ^foo=1(.*)$  [NC]
RewriteRule ^(.*)?foo=1$ $1foo=0 [R=301,L]

I would expect this to rewrite: example.com/abc.html/?foo=1 to: example.com/abc.html/?foo=0

Instead, it rewrites it to: example.com/home/[folder]/public_html/abc.html/?foo=0

(It adds file location path from home to the file abc.html. Htaccess file is located in public_html)

Can someone please tell me how I can prevent this?

Upvotes: 0

Views: 58

Answers (2)

anubhava
anubhava

Reputation: 785216

This should work for you:

RewriteEngine On

RewriteCond %{THE_REQUEST} \s/+([^?]+)\?(foo)=1[&\s] [NC]
RewriteRule ^ /%1?%2=0 [R=301,L]

Upvotes: 1

Howli
Howli

Reputation: 12469

Adding / before $1foo=0 [R=301,L] should solve that. So it would be

RewriteCond %{QUERY_STRING} ^foo=1(.*)$  [NC]
RewriteRule ^(.*)?foo=1$ /$1foo=0 [R=301,L]

Upvotes: 1

Related Questions