Forum Topic: QUERY_STRING values not displaying

Forum: .htaccess Forum : Redirecting • Posted by Gregory Bender • Updated:

Hi all,

I am attempting to redirect this URL:

http://www.example.com/old2.php?page=1

To this URL:

http://www.example.com/new2.php?main_page=products_new&disp_order=6&page=1

In the above example, “page=1”. But of course, there could be many page numbers. Rather than hard code all possibilities, I would like to simply “store” the page number as a variable and then rewrite using the page number.

Using this code:

RewriteCond %{REQUEST_URI} old2\.php [NC]
RewriteCond %{QUERY_STRING} ^page=(.*) [NC]
RewriteRule .* http://www.example.com/new2.php?main_page=products_new&disp_order=6&page=%2 [L,R=302]

I achieve this URL:

http://www.example.com/new2.php?main_page=products_new&disp_order=6&page=

No matter what I do, I cannot seem get the page number posted in the resulting URL. Any help would be greatly appreciated.

Regards,

Gregory Bender

3 Replies to “QUERY_STRING values not displaying”

Posted by Jeff Starr

I would try the following to troubleshoot:

1) Move the entire stanza of code to another location in the .htaccess file, try both before or after any other rules.

2) Try other strings besides ^page=(.*), just to make sure that the target URLs are even recognized.

3) Experiment with different pages/page-numbers, to see if the issue is with a particular page.

Hopefully that will yield some results. Good luck.

Posted by Gregory Bender •

Hi Jeff,

Thanks for the input. I tried all of your suggestions with no luck. Then, I discovered another approach that did work:

RewriteCond %{REQUEST_URI} old2\.php [NC]
RewriteCond %{QUERY_STRING} page=(.*) [NC]
RewriteRule .* http://example.com/new2.php?main_page=products_new&disp_order=6 [L,R=302,QSA]

The QSA copies the complete query string; &page=(.*) in this case. Thus, the resulting URL is what I needed:

http://www.example.com/new2.php?main_page=products_new&disp_order=6&page=1

For my current situation, this is a good, workable solution because *all* of my needed query string variables use the same name (in the above example, “page”). If, however, the variable names needed to change, this approach would not work at all. That is, if I needed to change page=1 to pg=1, this solution would not work.

I’m still looking into why I cannot get the %2 value to be written in the RewriteRule. If I figure something out, I’ll be sure to post it here.

Finally, thank you for your book. I spent days/hours Googling for solutions. Your book answered many questions for me in a short amount of time. You write well and your book was certainly worth my purchase. I will continue to reference it. Thanks.

Regards,

Gregory Bender

Posted by Jeff Starr

Ah! Excellent, glad you found a solution :)

Thank you for the positive feedback about the book, much appreciated!

Cheers!