Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

Joel Rivera
Joel Rivera
29,401 Points

Get Variables and Rewrite Rules

I followed the tutorial and I'm getting

Not Found The requested URL /shirts/shirts.php was not found on this server.

I am working on my local server on a macbook pro with Dreamweaver cs6

require_once("../includes/config.php"); require_once(ROOT_PATH . "includes/products.php");

include(ROOT_PATH . "includes/header.php"); ?>

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

<img src="<?php echo BASE_URL . $product["img"]; ?>" alt="<?php echo $product["name"]; ?>">

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

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

NOTE: On dreamweaver I am getting a message Dynamically related files could not be resolved because the site definition is not correct for this server.

Not sure what to do.

3 Answers

I managed to get this to work by removing the leading slash on the second part of the rewrite rule.

ie RewriteRule ^shirts/$ /shirts/shirts.php

becomes

RewriteRule ^shirts/$ shirts/shirts.php

Note the leading slash (/) on "shirts/shirts.php" has been removed.

This also applies to the line

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

I removed the leading slash (/) on /shirts/shirt.php?id=$1

to get

shirts/shirt.php?id=$1

The line will look like this

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

I hope this is of some help to you.

Daniel Leak
Daniel Leak
4,105 Points

It helped, felt like a dunce after realizing I'd done the exact same workaround on the first rewrite. Experimented with it after fixing and there seems to be some strange behaviour. I'm not sure if it's problems with the browser but I managed to get it to working with the second rewrite using a trailing slash, which caused the home and second shirts links to break.

Had site in secondary folder so put it into root folder, it didn't matter if there was or wasn't a trailing slash, or only one had it. I can't work out what's going on whatsoever.

Pavle Lucic
Pavle Lucic
10,801 Points

Mee to. I lost 2 fucking hours to figure out...

What are you using for your local PHP server?

I'm getting the same problem. I use Windows Premium 7 (with service pack 1), Mozilla Firefox 24.0.

I don't use dreamweaver, but everything else is the same. In .htaccess, when I remove line 3 (RewriteRule ^shirts/([0-9]+)/$ /shirts/shirt.php?id=$1), then I don't get that message of "Object not found". I've gone over the setup of my .htaccess file and checked the syntax for the command repeatedly.

I'm stuck how to get round this.

I looked into the log files.

From the apache error log file. I get this message: "D:/xampp/htdocs/EnhancingASimplePHPApplication/.htaccess: Invalid command 'Rewrite^Rule', perhaps misspelled or defined by a module not included in the server configuration "

The message talks about misspelling I have checked this repeatedly with the video. What modules are supposed to be included in the server configuration? How do I check them?

Here is the code from my .htaccess file: "RewriteEngine On RewriteRule ^shirts/$ /shirts/shirts.php RewriteRule ^shirts/([0-9]+)/$ /shirts/shirt.php?id=$1"

From the php error log I get: "[22-Oct-2013 21:38:19 Europe/Berlin] PHP Notice: Undefined variable: section in D:\xampp\htdocs\EnhancingASimplePHPApplication\inc\header.php on line 19"

Here is the code from line 19 in my header file. "<li class="contact <?php if ($section == "contact") {echo "on";} ?>"><a href="<?php echo BASE_URL ; ?>contact/">Contact</a></li>"

The only variable there is $section, which has been defined and assigned a value.

Any suggestions please?

Pavle Lucic
Pavle Lucic
10,801 Points

This video tutorial seems to work different from browser to browser.