How to disable QSA? (add query string)

I use Apache2 and mod_rewrite to hide query strings. These are the rules in question.

RewriteCond %{QUERY_STRING} ^query=(.*)$ RewriteRule (.*) /search/%1 [R=301,L] RewriteRule ^search\/?$ /search/?query=test [R=301,L] 

When I visit /search (or /search/ ), I am correctly redirected to /search/?query=test (according to the last rule)

From there, RewriteCond and RewriteRule should click and redirect me to /search/test , right? From what I understand, %1 in my first RewriteRule matches (.*) In the RewriteCond , which should contain test .

However, what is actually happening, I am redirected to /search/test/?query=test . So the rule works, but for some reason the query string is added. Is the QSA parameter automatically added somehow / somewhere?

Then I got stuck in an infinite redirect loop to /search/test?query=test , because the first RewriteCond and RewriteRule turn on again, and again, and again ...

What am I doing wrong?!

Thanks!

+4
source share
1 answer

You need to specify an empty query in the wildcard to add the original requested query to the new URL:

Query string change

By default, the query string is passed unchanged. However, you can create URLs in the lookup string that contains part of the query string. Just use the question mark inside the wildcard to indicate that the following text should be re-entered into the query string. If you want to delete an existing query string, complete the wildcard with only a question mark. To combine new and old query strings, use the [QSA] flag.

So:

 RewriteCond %{QUERY_STRING} ^query=(.*)$ RewriteRule (.*) /search/%1? [R=301,L] 
+11
source

All Articles