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 Get Variables and Rewrite Rules

Mark Ramage
Mark Ramage
8,875 Points

Rewrite Rule - Shirts

Hi,

I have the RewriteRule updating the URL to /shirts rather than /shirts.php, however the browser still displays the directory listing, rather than shirts.php.

Can you advise?

9 Answers

Mark Ramage
Mark Ramage
8,875 Points

Hi there,

Unfortunately I did not!

Not familiar with your server either.

Let me know if you get it working.

Thanks

You need to add a flag after your rewriterule to let the server know it should redirect permanently. Add [R=301] to the end of your rewriterule.

Mark Ramage
Mark Ramage
8,875 Points

Thanks for your reply, no luck though!

Mark Ramage
Mark Ramage
8,875 Points

Thanks for your reply, no luck though!

Matthew Underhill
Matthew Underhill
20,363 Points

Hi Mark,

Is your rewrite rule as follows?

RewriteRule ^shirts.php$ /shirts/ [R=301]

Mark Ramage
Mark Ramage
8,875 Points

Hi, it is indeed.

And, my first rule is as follows: RewriteRule ^shirts4mike/shirts/$ /shirts/shirts.php

I have the site in a sub folder of the root wamp/www folder called shirts4mike, with all my other projects.

Problem persists if I move all the site files to run from www/ and remove 'shirts4mike' from the rule, as in the demo.

Thank you.

AJ Singh
AJ Singh
18,769 Points

Hey Mark were you ever able to resolve this issue? I'm getting the same. I'm using Zend server on Windows

AJ Singh
AJ Singh
18,769 Points

One possible solution I am about to try is running the same local server that Randy uses (MAMP for Mac or XAMPP for Windows) on Ubuntu Linux. I normally use my Windows 7 Laptop with the Zend Server to follow along with the lessons. I partitioned some of my hard drive to have the Linux Operating system in the past. I'm gonna give XAMPP a try now. Hopefully the issue does not reoccur

UPDATE: It appears to be working with XAMPP. It doesn't matter that it is Windows or Linux. If you were on Mac Mamp would work well too.

"For rewrite rules to work, your web server has to have the software for rewrite rules enabled. Both mamp and xampp do by default."

So if you are having issues on another local server... that would be what to look into. I think the easiest method is just using xampp or mamp.

Good luck!

Mark Ramage
Mark Ramage
8,875 Points

Thanks for that!

I do have the rewrite rule enabled, I'm on WAMP, Windows 7.

I may come back to this.

geoffrey
geoffrey
28,736 Points

Hello there, I'm using Linux mint and I'm stuck at the same step. I checked if the rewrite rule was enabled with that command

sudo a2enmod rewrite

It says It's the case, however, when I click the shirts link in the header, it redirects me to the folder, the list of its content. In other words it doesn't work.

My .htaccess is located at the root of my shirt4Mike website and I typed the rule the same as the video.

RewriteEngine On

RewriteRule ^shirts/$ /shirts/shirts.php

Anyone would have a good solution ?