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

Thiago van Dieten
Thiago van Dieten
17,059 Points

Last 4 shirt pages broken ( Cleaning URLs with Rewrite Rules)

Hello,

I've successfully acquired my badge for "Cleaning URLs with Rewrite Rules" but in the end the latest 4 shirts have broken down.

Lemme tell you what i mean by showing the shirts.php file.

shirts.php

Working from a few subfolders, you can see on the bottom-left the link to the 1st shirt, localhost/treehouse/shirts4mike2/shirts4mike2_local/shirts/101/. Hovering on the other shirts reveal the same link with the correct product id (101-108).

However, while the first 4 shirts just work, the other 4 shirts return a 404 error as they get send to localhost/shirts, with shirts 107-108 even showing the wrong id, displaying 7 and 8: shirt 108

I have failed to notice the pattern which is causing so I hope you can help me with this problem. What i'm gonna share below are the code of .htaccess (in the shirts4mike2_local folder), products.php and the config.php file.

.htaccess

RewriteEngine On
RewriteBase /treehouse/shirts4mike2/shirts4mike2_local/

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

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

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;
}

$products = array();
$products[101] = array(
    "name" => "Logo Shirt, Red",
    "img" => "img/shirts/shirt-101.jpg",
    "price" => 18,
    "paypal" => "9P7DLECFD4LKE",
    "sizes" => array("Small","Medium","Large","X-Large")
);
$products[102] = array(
    "name" => "Mike the Frog Shirt, Black",
    "img" => "img/shirts/shirt-102.jpg",
    "price" => 20,
    "paypal" => "SXKPTHN2EES3J",
    "sizes" => array("Small","Medium","Large","X-Large")
);
$products[103] = array(
    "name" => "Mike the Frog Shirt, Blue",
    "img" => "img/shirts/shirt-103.jpg",    
    "price" => 20,
    "paypal" => "7T8LK5WXT5Q9J",
    "sizes" => array("Small","Medium","Large","X-Large")
);
$products[104] = array(
    "name" => "Logo Shirt, Green",
    "img" => "img/shirts/shirt-104.jpg",    
    "price" => 18,
    "paypal" => "YKVL5F87E8PCS",
    "sizes" => array("Small","Medium","Large","X-Large")
);
$products[105] = array(
    "name" => "Mike the Frog Shirt, Yellow",
    "img" => "img/shirts/shirt-105.jpg",    
    "price" => 25,
    "paypal" => "4CLP2SCVYM288",
    "sizes" => array("Small","Medium","Large","X-Large")
);
$products[106] = array(
    "name" => "Logo Shirt, Gray",
    "img" => "img/shirts/shirt-106.jpg",    
    "price" => 20,
    "paypal" => "TNAZ2RGYYJ396",
    "sizes" => array("Small","Medium","Large","X-Large")
);
$products[107] = array(
    "name" => "Logo Shirt, Teal",
    "img" => "img/shirts/shirt-107.jpg",    
    "price" => 20,
    "paypal" => "S5FMPJN6Y2C32",
    "sizes" => array("Small","Medium","Large","X-Large")
);
$products[108] = array(
    "name" => "Mike the Frog Shirt, Orange",
    "img" => "img/shirts/shirt-108.jpg",    
    "price" => 25,
    "paypal" => "JMFK7P7VEHS44",
    "sizes" => array("Large","X-Large")
);

?>

config.php

<?php

define('BASE_URL', '/treehouse/shirts4mike2/shirts4mike2_local/');
define('ROOT_PATH', $_SERVER["DOCUMENT_ROOT"] . '/treehouse/shirts4mike2/shirts4mike2_local/');

Thanks for taking the time to read all of this! :)

1 Answer

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

To make sure I understand the problem, do you see the shirt details page correctly when you go here?

But not when you go here?

I'm looking through the code now to see if anything stands out.

Thiago van Dieten
Thiago van Dieten
17,059 Points

Nope, when i go to

I'll get localhost/shirts/105 in my browser bar, same counts for 106. For 107 and 108, it's says localhost/shirts/7 and 8.

(Section where i check if Firebug gives me the HTTP 301 code when i visit those pages)