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 trialtzehuilee
19,911 PointsTrouble 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
2 Answers
thomascawthorn
22,986 PointsWait 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
shirts.php
But I think rule 2 should rewrite to
shirt.php
notice the missing 's'!!
Therefore rule 2 should be:
RewriteRule ^shirts/([0-9]+)/$ /shirts4mike/shirts/shirt.php?id=$1
thomascawthorn
22,986 PointsHmm.. It would seem that you're redirecting both /shirts/ and shirts/1/ to shirts.php
Should shirts/1/ not redirect to shirt.php?
tzehuilee
19,911 PointsI'm not sure you what you're implying. What I am trying to do is to create clean URL for the detail shirt page. Each detail shirt has its own id number in the array and is identified by shirt.php?id=id_number. For example if you click a shirt that has the id of 108. The URL would be localhost//shirts4mike/shirts/shirt.php?id=108 . With the clean URL it should be localhost//shirts4mike/shirts/108/ . If you check the lesson video(Get Variables and Rewrite Rules) under Cleaning URLs with Rewrite Rules , you'll know what I mean. The trouble is that when I click any shirt with id=108 it takes me to localhost/shirts4mike/shirts/ but with URL ocalhost//shirts4mike/shirts/108/ .
Here are the PHP files with segment of codes which I believe that might be the culprit. I still wouldn't over look .htaccess and config.php mentioned above.
1/ products.php
<?php
function get_list_view_html($product_id, $product) {
$output = "";
$output = $output . "<li>";
$output = $output . '<a href="' . BASE_URL . 'shirts/' . $product_id . '/">';
$output = $output . '<img src="' . BASE_URL . $product["img"] . '" alt="' . $product["name"] . '">';
$output = $output . "<p>View Details</p>";
$output = $output . "</a>";
$output = $output . "</li>";
return $output;
}
2/ index.php(main file of the source folder)
<?php include(ROOT_PATH . "inc/products.php"); ?>
<ul class="products">
<?php
$total_products = count($products);
$position = 0;
$list_view_html = "";
foreach($products as $product_id => $product) {
$position = $position + 1;
if ($total_products - $position < 4) {
$list_view_html = get_list_view_html($product_id,$product) . $list_view_html;
}
}
echo $list_view_html;
?>
</ul>
3/shirts.php
pageTitle = "Mike's Full Catalog of Shirts";
$section = "shirts";
include(ROOT_PATH . 'inc/header.php'); ?>
<div class="section shirts page">
<div class="wrapper">
<h1>Mike’s Full Catalog of Shirts</h1>
<ul class="products">
<?php foreach($products as $product_id => $product) {
echo get_list_view_html($product_id,$product);
}
?>
</ul>
</div>
That's lots of codes posted, but I am totally lost and cannot continue further with the project, unless the bug is fixed. Hopefully Randy will stumble on my post and offer me the solution. But any PHP ace will do.
tzehuilee
19,911 Pointstzehuilee
19,911 PointsIt worked!! Thanks! Never thought a mistypo could cause such hassle.