Welcome to the Treehouse Community
Looking to learn something new?
Trouble with .htaccess file. Cannot get to detail shirt page after modifying rewrite rules.
I am using Netbeans and XAMMP on this project. The source folder is shirts4mike. So the URL is localhost/shirts4mike.
As shown in the video, I tried to get to the detail shirt page by typing localhost/shirts4mike/shirts/108/.
Instead it takes me to "MIKE’S FULL CATALOG OF SHIRTS" page (localhost/shirts4mike/shirts/). The URL still reads localhost/shirts4mike/shirts/108/ even with 102, 103, or 104 at the end etc. Doesn't matter what shirt I click, it will always end in "MIKE’S FULL CATALOG OF SHIRTS" page with the shirt id number at the end of the URL.
I thought there was something wrong with shirt.php or products.php. I downloaded the project file and paste and overwrite all my original files except .htaccess and config.php. The result is still the same above.
My config.php is read:
<?php define("BASE_URL", "/shirts4mike/"); define("ROOT_PATH", $_SERVER["DOCUMENT_ROOT"] . "/shirts4mike/"); ?>
I think there might something wrong with my .htaccess file. The code/rules for it is:
RewriteEngine On RewriteBase /shirts4mike RewriteRule ^shirts/$ /shirts4mike/shirts/shirts.php RewriteRule ^shirts/([0-9]+)/$ /shirts4mike/shirts/shirts.php?id=$1
Wait wait wait wait wait - relax - I think this is a simple typo.
I'm going on the assumption that you have two files - shirts.php to show many shirts and shirt.php to show a single shirt.
This is your code:
RewriteEngine On RewriteBase /shirts4mike #rule 1 RewriteRule ^shirts/$ /shirts4mike/shirts/shirts.php #rule 2 - should this say shirt.php instead of shirts.php? RewriteRule ^shirts/([0-9]+)/$ /shirts4mike/shirts/shirts.php?id=$1
in rule 1, you're saying anything that looks like this: "shirts/" should be rewritten to "/shirts4mike/shirts/shirts.php". This is where you're showing many shirts. This looks fine.
in rule 2, you're saying anything that looks like "shirts/([0-9])" should be rewritten to "/shirts4mike/shirts/shirts.php?id=$1". But this is the page where you're showing many shirts (shirts.php). Surely you want to redirect to shirt.php, which is your page is display a single shirt.
In both rules you're rewriting to
But I think rule 2 should rewrite to
notice the missing 's'!!
Therefore rule 2 should be:
RewriteRule ^shirts/([0-9]+)/$ /shirts4mike/shirts/shirt.php?id=$1
Hmm.. It would seem that you're redirecting both /shirts/ and shirts/1/ to shirts.php
Should shirts/1/ not redirect to shirt.php?