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

Help! Redirect .htaccess is not working.

Sorry for the very long post, but I need to understand why my shirts.php and shirt.php file will give me a 404 browser error even with the use of rewrite rules and redirect rules. If I give the absolute path, then the file opens but I have to write the whole address in the browser.

localhost/shirts4mike/shirts/shirts.php

When I encountered this problem I reviewed other threads to see if anyone had a similar problem. I saw some similarities, but the answers didn't help me very much. I need a different perspective, that might help me find the solution. So far this is what I have done from reading previous posts.

  1. I have checked the httpd.config on my mamp server to see if I had it to AllowOverride All. it was set to None, I changed it to all -> restarted the local server.
  2. I moved the .htaccess to /shirts, in a earlier experiment it worked, but then when I added more rules it stopped, so I proceeded to place it back in the /shirts4mike folder
  3. I checked to see if my links were not using the base URL correctly. There is nothing wrong with the code (or at least I think there is not). Below is the code for my shirt.php page
<?php 
require_once("../inc/config.php");
require_once(ROOT_PATH . "inc/products.php");?>
if (isset($_GET["id"])) {
    $product_id = $_GET["id"];
    if (isset($products[$product_id])) {
        $product = $products[$product_id];
    }
}
if (!isset($product)) {
    header("Location:". BASE_URL ." shirts/");
    exit();
}

$section = "shirts";
$pageTitle = $product["name"];
include(ROOT_PATH . "inc/header.php"); ?>

        <div class="section page">

            <div class="wrapper">

                <div class="breadcrumb"><a href="<?php echo BASE_URL;?>shirts/">Shirts</a> &gt; <?php echo $product["name"]; ?></div>

                <div class="shirt-picture">
                    <span>
                        <img src="<?php echo BASE_URL . $product["img"]; ?>" alt="<?php echo $product["name"]; ?>">
                    </span>
                </div>

                <div class="shirt-details">

                    <h1><span class="price">$<?php echo $product["price"]; ?></span> <?php echo $product["name"]; ?></h1>

                    <form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
                        <input type="hidden" name="cmd" value="_s-xclick">
                        <input type="hidden" name="hosted_button_id" value="<?php echo $product["paypal"]; ?>">
                        <input type="hidden" name="item_name" value="<?php echo $product["name"]; ?>">
                        <table>
                        <tr>
                            <th>
                                <input type="hidden" name="on0" value="Size">
                                <label for="os0">Size</label>
                            </th>
                            <td>
                                <select name="os0" id="os0">
                                    <?php foreach($product["sizes"] as $size) { ?>
                                    <option value="<?php echo $size; ?>"><?php echo $size; ?> </option>
                                    <?php } ?>
                                </select>
                            </td>
                        </tr>
                        </table>
                        <input type="submit" value="Add to Cart" name="submit">
                    </form>

                    <p class="note-designer">* All shirts are designed by Mike the Frog.</p>

                </div>

            </div>

        </div>

<?php include(ROOT_PATH . "inc/footer.php"); ?>

This is how my .htaccess file looks like

RewriteEngine On
RewriteRule ^shirts/$ /shirts/shirts.php
RewriteRule ^shirts/([0-9]+)/$ /shirts/shirt.php?id=$1
RewriteRule ^receipt.php$ /receipt/ [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]

Please help, I have watched the videos again, I have looked over my notes, but nothing seems to be working. WIthout leaving out that my contact/ redirect is working.

1 Answer

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

RewriteRule ^receipt.php$ /receipt/ [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]

Here's my code which is 100% working.. trouble is I can't sport any differences between mine and yours! Sometimes my redirects don't start working until after I clear all of my browser history and cache etc.

If you're getting a 404, the redirect sounds like it's working - but it can't find the destination file so maybe double check all the files have the right names/extensions.

I verified the destination file, and it is correct, still get the 404 error.