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

Rewrite Rule doesn't work (PHP course)

Hey,

I'm trying to solve this problem for a few days now. I also posted in an older subject, but nobody responded, so I decided to create my own post (I hope that doesn't cause any problem). So, these are my rewrite rules:

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

I use XAMPP, I have my .htaccess at the root of htdocs directory. I'm sure .htaccess is read because I get an error if I write something strange. The mod_rewrite was already enabled and I changed the AllowOveride to All. I did restart Apache after the changes.

But still, if I go to localhost/shirts, I get the "menu" with the two files. If I go to shirts/shirts.php, it does load but with the images, which is different from the video (Cleaning URLs with Rewrite Rule).

Did I miss something?

1 Answer

Placid Rodrigues
Placid Rodrigues
12,630 Points

Hi Jorge,

First of all, you need to remove the slash after the ^ sign.

So, if your project is directly in the htdocs folder, the rewrite rule should be:

RewriteRule ^shirts/$ /shirts/shirts.php

If your project is in any sub-folder of the htdocs folder, you need to include the sub-folder in your rule. Like this:

RewriteRule ^shirts/$ /subfolder/shirts/shirts.php

Hope that helps.

Hi,

Thanks for answering! I did this, but it's still the same, do you have any other idea? There's another thing, but I don't know if it's relevant or not. I have two files in this shirts subfolder, shirts and shirt_detail. Whenever I try to go to shirt_detail, I'm redirected to shirts, so i suppose my rewrite rule somewhat works..

Placid Rodrigues
Placid Rodrigues
12,630 Points

Can you upload your files and folders in a workspace and share the link?

All of them? Or just the shirts folder and the .htacces?

Placid Rodrigues
Placid Rodrigues
12,630 Points

What's the name of your project folder?

PHP - Frog Site I'm not sure how to share the link of a workspace, sorry :/

Placid Rodrigues
Placid Rodrigues
12,630 Points

I don't understand. Your htaccess file shows as _htaccess instead of .htaccess.

Placid Rodrigues
Placid Rodrigues
12,630 Points

I think your shirt_detail.php page is redirecting to shirts.php with header('Location: shirts.php') that's inside your shirt_detail.php file. It is not due to RewriteRule.

Oh crap, I wrote it the correct way the first time, but i did a lot of changes after and didn't see this. Thank you very much!!