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 Enhancing a Simple PHP Application Cleaning URLs with Rewrite Rules Rewrite Rules with Query Strings

Tom Mertz
Tom Mertz
15,254 Points

RewriteCond not working with Query String

I'm working my way through PHP and I'm getting stuck on the RewriteCond for the shirts4mike website, where the address shirt.php?id=104 should redirect to /shirts/104/

My .htaccess file is the following:

RewriteEngine On
RewriteRule ^shirts4mike/shirts/$ /shirts4mike/shirts/shirts.php
RewriteRule ^shirts4mike/shirts/([0-9]+)/$ /shirts4mike/shirts/shirt.php?id=$1
RewriteRule ^shirts4mike/receipt.php$ /shirts4mike/receipt/ [R=301]
RewriteRule ^shirts4mike/contact.php$ /shirts4mike/contact/ [R=301]
RewriteRule ^shirts4mike/shirts.php$ /shirts4mike/shirts/ [R=301]
RewriteRule ^(shirts4mike/shirts/[0-9]+)$ /$1/ [R=301]

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

What is really frustrating is that the code works for one page, haha. When I enter shirt.php?id=103, I get shirts/103/

But for any other id, it will not redirect.

Side note, my directory looks like this:

htdocs
   .htaccess
   shirts4mike/
      index.php
      contact/
      receipt/
      shirts/
      etc...

Any help would be appreciated!

Tom

jack hamilton
jack hamilton
5,609 Points

Yeah bit of a grave dig but I've got this problem as well.

1 Answer

Tom Mertz
Tom Mertz
15,254 Points

Hey Jack,

I read some other people's comments on other posts in the forums and it seems like chrome caches the url if you type it in before you finished the .htaccess file. Try deleting your cache and see if it works.

Alternatively try it in Firefox or IE.

Hope you can get it to work!