1

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]
MrWhite
  • 43,179
  • 8
  • 60
  • 84

1 Answers1

1

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]
arkascha
  • 41,620
  • 7
  • 58
  • 90
  • `^(.*)&?css=(mobile|desktop)(.*)$` - You'll need a couple of tweaks to the _CondPattern_... The first capturing group needs to be non-greedy, so that it doesn't consume the optional `&` and the middle _alternation_ group should be non-capturing (since you've used `%1` and `%2` in the _substitution_ string). In other words: `^(.*?)&?css=(?:mobile|desktop)(.*)$`. – MrWhite Jan 24 '19 at 00:39