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 Enhancing a Simple PHP Application Cleaning URLs with Rewrite Rules Introducing Rewrite Rules

Becky Christofferson
Becky Christofferson
15,047 Points

Rewrite Rule directs to a 404 Error

I'm trying to redirect from shirts to shirts/shirts.php. My current Rewrite code is:

RewriteEngine On RewriteRule ^shirts/$ /shirts/shirts.php

It is directing me to a 404 Error. I'm working on a windows system, the web server is xampp and my current folder structure is:

htdocs/shirts4mike/shirts/shirts

Any suggestions?

Becky Christofferson
Becky Christofferson
15,047 Points

I have also checked the config file and the rewrite module is running, so I don't believe that's the problem.

2 Answers

Aaron Lafleur
Aaron Lafleur
10,474 Points

Check your XAMPP error logs...I found that tool extremely helpful in the past. On your control panel, click "Logs" in the Apache row, then PHP error logs. Might give you a hint.

Also, I had nothing but trouble getting the site to work properly when my root was 'shirts4mike'. There are all sorts of include files and such to update in order to follow along with Randy's course, that I opted to leave htdocs as the root. Things went alot better afterwards. I have a feeling .htaccess works with htdocs as your root also. Could be wrong. I'm no pro.

Good luck!

Aaron Munoz
Aaron Munoz
11,177 Points

I suggest you not answer a question if you really don't know the answer. Your response created more confusion.

Luis Vazquez
Luis Vazquez
6,370 Points

I might be too late in replying but I bumped into this same issue. Looks like .htaccess will use your 'htdocs' folder as the root directory. You can add a "RewriteBase" rule for a .htaccess file in sub-folders.

In your case the .htaccess file would look like this:

RewriteEngine On
RewriteBase /shirts4mike/ 

RewriteRule ^shirts/$ shirts/shirts.php

In a real situation, you'd add that RewriteBase rule during testing and once you deploy the site into production, you'd remove it. Great way to test .htaccess files without having to have every site within the htdocs folder - especially if you're working on more than one site :-)

-Luis

This worked for me! Thanks.

Yes, this helped me as well. Thanks, Luis!

Rifqi Fahmi
Rifqi Fahmi
23,164 Points

thanks, it work. you should be the best answer :D