Welcome to WebmasterWorld Guest from 54.159.242.217

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)

New User

10+ Year Member

joined:May 15, 2005
posts:24
votes: 0


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:

/product_info.php?products_id=63

to

/product_info.php/products_id/63

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)

Junior Member

10+ Year Member

joined:Nov 12, 2005
posts:136
votes: 0


Try [L] instead of [R=301,L]
6:44 am on Jan 8, 2006 (gmt 0)

Senior Member

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

joined:Mar 31, 2002
posts:25430
votes: 0


ambition,

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.

Jim

[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)

New User

10+ Year Member

joined:May 15, 2005
posts:24
votes: 0


Jim,

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)

Senior Member

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

joined:Mar 31, 2002
posts:25430
votes: 0


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.

Jim

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members