Forum Moderators: phranque

Message Too Old, No Replies

Changing ? into & in rewrite

         

ocon

4:37 pm on Nov 7, 2010 (gmt 0)

10+ Year Member Top Contributors Of The Month



Hello. I'm using the following rewrite:

rewrite ^/dir1/(.*)$ /dir2/index.php?title=$1

If someone tries accessing: /dir1/Page_title
Then the page served is: /dir2/index.php?title=Page_title

This works great, but sometimes scripts try to add parameters to the pages (like Google Analytics when viewing traffic flow).

They might try to load up: /dir1/Page_title?cPath=abc&location=xyz
Then the page served is: /dir2/index.php?title=Page_title?cPath=abc&location=xyz

This is a problem because I have two question marks, and the parameters are not passing correctly.

What I would like to do is to modify my rewrite rule to change any ? into &

Then the scripts might try to load up: /dir1/Page_title?cPath=abc&location=xyz
But the page served is: /dir2/index.php?title=Page_title&cPath=abc&location=xyz

sublime1

5:56 pm on Nov 7, 2010 (gmt 0)

10+ Year Member



I assume your example is a RewriteRule (you have "rewrite"). If so, check out the Query String Append (QSA), which should do the trick for you.

Tom

g1smd

6:00 pm on Nov 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Use the [QSA,L] flags here.

Having specified new parameters in the rewrite, you have over-ruled the automatic re-appending of the original parameters. Use the QSA flag to re-append them. Use the L flag too, otherwise you might see odd behaviour for some requests.

ocon

2:17 pm on Nov 8, 2010 (gmt 0)

10+ Year Member Top Contributors Of The Month



Thank you very much, that works perfectly. I would have thought the solution would have been much more complex. On a side note, when finding what the equivalent flags were for nginx, I found it was already automatic for rewrites!

ocon

2:23 pm on Nov 8, 2010 (gmt 0)

10+ Year Member Top Contributors Of The Month



:Sorry, posted message twice: