Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

how make the rewritecond work with the next path: http://someurl.com/shirts/shirt.php?id=101

I follow the video and this is the result:

RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^shirt.php$ /someurl/shirts/%1/? [R=301]

but this just work, when you access on this way: http://someurl.com/shirt.php?id=101 and then redirect to: http://someurl.com/shirts/101/

Instead, I want to access with this way: http://someurl.com/shirts/shirt.php?id=101 and then redirect to: http://someurl.com/shirts/101/

I tried to figure how, with this:

RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^shirts/shirt.php$ /someurl/shirts/%1/? [R=301]

but this created a infinited redirect loop in my site, when i tried to access: http://someurl.com/shirts/shirt.php?id=101 and then infinite redirect to: http://someurl.com/shirts/101/

if someone can help me how I can solve this, I'll be appreciate. :)

By the way, there is the rest of my .htaccess file:

RewriteEngine On
RewriteRule ^shirts/$ /someurl/shirts/shirts.php
RewriteRule ^shirts/([0-9]+)/$ /someurl/shirts/shirt.php?id=$1

RewriteRule ^contact.php$ /someurl/contact/ [R=301]
RewriteRule ^receipt.php$ /someurl/receipt/ [R=301]
RewriteRule ^shirts.php$ /someurl/shirts/ [R=301]
RewriteRule ^(shirts/[0-9]+)$ /someurl/$1/ [R=301]

RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^shirt.php$ /someurl/shirts/%1/? [R=301]

1 Answer

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

It looks like this should cause an infinite redirect. Line 3 redirects this ...

http://someurl.com/shirts/101/

... to this ...

http://someurl.com/shirt.php?id=101 

... and then the last two lines redirect it back to ...

http://someurl.com/shirts/101/

... and then Line 3 kicks in and redirects it back to ...

http://someurl.com/shirt.php?id=101 

... and then the last two lines kick in and redirects it back to ...

http://someurl.com/shirts/101/

... and so on.

Does that make sense? You probably want to remove either Line 3 or the last two lines to prevent the redirect you don't want. Does that help?