Reputation: 11
On every page of my website at the end of the URL could be ?css=(mobile|desktop)
query. I need to delete this query like this:
example.com/?css=mobile
to example.com
example.com/dir1?css=mobile
to example.com/dir1
example.com/dir1/.../dir10?css=mobile
to example.com/dir1/.../dir10
I tried to do it like this, but I can't make the right rule.
RewriteCond %{QUERY_STRING} css=(mobile|desktop)
RewriteRule ^(.*) problemhere [R=301,L]
Upvotes: 1
Views: 59
Reputation: 42984
I'd say the issue here is that you need to preserve other potential get parameters...
Probably something like that might work:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(.*)&?css=(mobile|desktop)(.*)$
RewriteRule ^/?(.*)$ /$1?%1%2 [R=301,L,QSD]
That rule set should work likewise in the http servers host configuration and also in dynamic confioguration files (".htaccess" style files) if you have to use those (which you should try to prevent...).
Here is a modified version with a fixed condition as pointed out by @MrWhite in the comment:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(.*?)&?css=(?:mobile|desktop)(.*)$
RewriteRule ^/?(.*)$ /$1?%1%2 [R=301,L,QSD]
Upvotes: 1