2

I have these rewrites. The first (for gallery) works as expected. The second (for photo) works but the query string is repeated. So it forwards to: http://www.domain.com/photo-TheID?id=TheID

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /gallery\.php\?set=([^/]*)\ HTTP/
RewriteRule ^gallery\.php$ http://www.domain.com/gallery?set=%1 [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /photo\.php\?id=([^/]*)\ HTTP/
RewriteRule ^photo\.php$ http://www.domain.com/photo-%1 [R=301,L]

However, if I add ? to the end of the rewriterule making it

RewriteRule ^photo\.php$ http://www.domain.com/photo-%1? [R=301,L]

It then works as expected forwarding to: http://www.domain.com/photo-TheID

My question is why is that query string being repeated without the "?" at the end? It's very confusing to me since the first rewrite (for gallery) does not have the ? yet it does not repeat the query string. If I add ? to the end of the gallery rewriterule it adds "%3f" to the end of the url.

0

1 Answer 1

1

Because it is appended automatically unless you place a trailing question mark in the substitution URL, provided it doesn't hold a new query string.

"When you want to erase an existing query string, end the substitution string with just a question mark".

Check the title Modifying the Query String in this Apache link.

Your question:

It's very confusing to me since the first rewrite (for gallery) does not have the ?

The reason is the first rewrite rule:

RewriteRule ^gallery\.php$ http://www.domain.com/gallery?set=%1 

creates a new query string and in that case the incoming query string is not appended automatically unless you explicitly do it with the QSA flag.

Sign up to request clarification or add additional context in comments.

Comments

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.