0

I have a URL that i want to rewrite but want to keep the Query String as it is. I tried several options but not working.

http://www.example.com/images/cam?fileName=123.jpg&format=jpeg I want this to rewrite to http://www.example.com/new-images/cam?fileName=123.jpg&format=jpeg

I am using following rewrite rule

RewriteRule ^images/cam(.*)$ /new-images/cam$1 [L]

What am i missing. Thanks in advance.

2 Answers 2

1

You can use:

RewriteRule ^images/(cam/?)$ new-images/$1 [L,NC]

QUERY_STRING will be automatically carried over to new target URI.

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

3 Comments

I'm afraid, "Use of the [NC] flag causes the RewriteRule to be matched in a case-insensitive manner." That won't effect the query string. The "default behavior of RewriteRule is to discard the existing query string". So only the QSA flag will solve this, as I posted in my answer.
@Raphael: It is time to learn more about QSA flag. QSA is needed only when you're modifying query string in the target URI and this is not the case with rule. It is better to test it out on your local Apache my suggested rule to understand better.
You're right. QSA shouldn't be needed, since there is no query string in the rewrite. In my environment all three versions work well. So there should be another problem.
0

You can set the QSA flag to append the original query string to the rewrite:

RewriteRule ^images/cam(.*)$ /new-images/cam$1 [L,QSA]

(see: http://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_qsa)

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.