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

Introducing Rewrite Rules issue on localhost...

Hey, So i'm working on the php application project, and i've got a problem atm with the paths... My folder structure is :

htdocs/treehouse/shopmike/allwebsitecontenthere

So in order to make the rewrite rules working on htacess what exactly should i write on the htaccess file? i've tried multiple solutions, but none seem to work. Everytime i try to acess the shirts folder, i just get the "no index in this directory" error. Here's my code atm on htaccess:

    RewriteEngine On
    RewriteRule ^/treehouse/shopmike/shirts/$ /treehouse/shopmike/shirts/shirts.php
    RewriteRule ^/treehouse/shopmike/shirts/([0-9]+)/$ /treehouse/shopmike/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]

if i manually type "http://localhost/treehouse/shopmike/shirts/shirts.php" i go to the shirts page, but without the images. but if i manually type "http://localhost/treehouse/shopmike/shirts/shirt.php" i get the error code. I'm really at a loss here, so any help on this would be awesome

Thx in advance,

Bruno

12 Answers

Don Shipley
Don Shipley
19,488 Points

Try this

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]

From his code remove the / before shirts etc.

That worked!!! if i manually type "http://localhost/treehouse/shopmike/shirts/shirts.php" it works. if i manually type "http://localhost/treehouse/shopmike/shirts/shirt.php" i get "http://localhost/treehouse/shopmike/shirts/" If i select one of the shirts to view it's details i get "http://localhost/treehouse/shopmike/shirts/101/" And lastly if i manually enter a tshirt id (as in instead of 101 i write 108) it works just the same and displays the id of that shirt instead of 101. If i enter an unknown id i end up back at "http://localhost/treehouse/shopmike/shirts/".

So yeah, everything working perfectly now. Thx alot mate.

Just found out that the redirection of the old URL's isn't working... Meaning:

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]

this part isn't working... If i manually type "http://localhost/treehouse/shopmike/receipt.php" i get redirected to "http://localhost/Applications/XAMPP/xamppfiles/htdocs/treehouse/shopmike/receipt/" and get a 404 error... Any idea why this part of the code isn't working?

Don Shipley
Don Shipley
19,488 Points

I checked my now as well. Have the same issue. Must be in the RewriteRule not sure where. I have had a lot of problem with the codes still learning myself. Wish I could help you on this one..

I know that having the Shirts4mike project inside 2 folders is making this harder to understand... If i had the project exactly like Randy has on the tutorial it would be easier to understand, but still i'd like to see if i can figure out how to do this with my current folder structure...

Oh well, let's hope someone give us a bit of help on this issue...

Don Shipley
Don Shipley
19,488 Points

Randy gave this link in one of his tutorials where you can study more on the .htaccess. This is in the .htaccess https://kb.mediatemple.net/questions/85/Using+.htaccess+rewrite+rules#gs/-regular-expressions--

After learning how to store the info into Mysql ther are other ways in getting the files into the site.

Thx for the link. Gonna have a look as soon as i get home from work.

I read the stuff on that link, but i'm still struggling with these Rewrite Rules... I'm still getting that 404 error if i for example type "http://localhost/treehouse/shopmike/receipt.php", of i try to type a shirt id without the closing slash "http://localhost/treehouse/shopmike/shirts/102"

Anyone else can give me a hand with this? I wonder if Randy is on vacation and that's why he's not answering me on this topic :P

Don Shipley
Don Shipley
19,488 Points

Send support this link for the forum and ask support to help. Would like to see the problem.

Yeah might as well do that, since we're getting no help here in the forums. Thx for the help though Don.

Hello Bruno Feijão & Don Shipley.

You guys are on the right track! Rewrite rules can be very challenging so hang in there.

Here is a solution I used in testing, see if this works for you.

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


RewriteCond %{QUERY_STRING} ^status=thanks
RewriteRule ^contact.php$ contact/thanks/?
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 ^shirts.php$ shirts/%1/? [R=301]

One thing to note, there are several different ways to solve a single problem, so use what works best for you.

You can also test your rewrite rules on this htaccess tester

Let me know how this goes.

Good Luck

Don Shipley
Don Shipley
19,488 Points

Thank you, Hampton. I really love the link to the .htaccess tester. One thing in your code I noticed RewriteCond %{QUERY_STRING} ^status=thanks RewriteRule ^contact.php$ contact/thanks/? RewriteRule ^contact.php$ contact/ [R=301] looks great I am gong to try this on my site to see if this will redirect my header to show the thank you message. For now I removed the contact page for after you submitt the form it shows a white page.

Thx for the answer Hampton. I tried your code and i still have the same issue. Going to a single shirt page and removing the forward slash at the end still gives me an error. Same as typing receipt.php instead of only receipt. Either way the site is working fine so this isn't a deal breaker, since i don't see many ppl visiting the website and purposely removing the forward slash or writing receipt.php...

I think i'm letting this one go by for now, since it's not a deal breaker. Once i have more time to play around with the code i might give it another shot at it...

Thank you both for your help, much appreciated.

Michael Fraser
Michael Fraser
19,084 Points

Hi, I'm having grief with this as well; I've got the project in a sub folder on localhost (MAMP), and I can't access any t-shirt detail pages; the URL gets rewritten to the shirts/ page. Tried the code by Hampton, and had a play on the htaccess tester linked above, but no joy.