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]

1 Answer 1

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]
Sign up to request clarification or add additional context in comments.

1 Comment

^(.*)&?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)(.*)$.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.