Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Rewriting URLs with query strings

Can't get it to work



6:42 pm on Jan 7, 2006 (gmt 0)

10+ Year Member

Hey all,

I've been reading in this forum about this, but I am unable to get it to work exactly.

What I want to do it rewrite urls with a? to a url without.

For example:




This is what I have so far in my .htaccess (from another post on this thread):

RewriteCond %{QUERY_STRING} products_id=([0-9]+)
RewriteRule ^product_info.php product_info.php/products_id/%1 [R=301,L]

This seems to cause an infinite loop. Any ideas?


11:11 pm on Jan 7, 2006 (gmt 0)

5+ Year Member

Try [L] instead of [R=301,L]


6:44 am on Jan 8, 2006 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member


Welcome to WebmasterWorld!

It causes an infinite loop because you did not clear the query string -- See the "?" I added to the substitution URL.

If you want to clear the query internally, so product_info.php doesn't see it in STDIN, I'd suggest:

RewriteCond %{QUERY_STRING} ^products_id=([0-9]+)$
RewriteRule ^product_info\.php$ /product_info.php/products_id[b]/%1? [L][/b]

However, if you want search engines to drop this URL, then you'll need to use a redirect:

RewriteCond %{QUERY_STRING} ^products_id=([0-9]+)$
RewriteRule ^product_info\.php$ http://www.example.com/product_info.php/products_id/%1? [R=301,L]

Finally, if you are wanting to implement search-engine-friendly URLs, then both of these methods are backwards; You'll need to change the URLs on your pages to the static form, and then rewrite those to the proper form needed to call your script. I.e., publish the link http://www.example.com/products/product63 on your pages, and then use

RewriteRule ^products/product([0-9]+)$ /product_info.php?products_id=$1 [L]

to translate that URL, when requested from your server, back to a script call.


[edit] Corrected as noted below in msg#4 for future reference. [/edit]

[edited by: jdMorgan at 8:56 pm (utc) on Jan. 8, 2006]


7:47 pm on Jan 8, 2006 (gmt 0)

10+ Year Member


This is SO much help!

I have already changed my real urls to be search engine friendly, so I am just looking to get the old urls out of the search engines with a 301 redirect. So your 2nd code example was perfect (although it took me a minute to figure out to remove the 2nd ) in the RewriteCond statement which I'm sure was a just a typo).

Using these examples I was able to rewrite several other urls that I had been working on to get 301'd too. I am slowly starting to understand how the RewriteCond statement works.

Thanks again man, I owe you a beer or three. :)


8:57 pm on Jan 8, 2006 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

Oops! Yes, a typo -- and cut-and-pasted into both examples, too. Sorry about that!

I corrected it so people looking at this thread in the future won't have problems, too.



Featured Threads

Hot Threads This Week

Hot Threads This Month