Артем Артем
Артем Артем

Reputation: 11

How to remove query string from URL with htaccess rule?

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:

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

Answers (1)

arkascha
arkascha

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

Related Questions