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 trialBruno Feijão
Courses Plus Student 4,345 PointsIntroducing 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
19,488 PointsTry 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.
Bruno Feijão
Courses Plus Student 4,345 PointsThat 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.
Bruno Feijão
Courses Plus Student 4,345 PointsJust 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
19,488 PointsI 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..
Bruno Feijão
Courses Plus Student 4,345 PointsI 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
19,488 PointsRandy 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.
Bruno Feijão
Courses Plus Student 4,345 PointsThx for the link. Gonna have a look as soon as i get home from work.
Bruno Feijão
Courses Plus Student 4,345 PointsI 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
19,488 PointsSend support this link for the forum and ask support to help. Would like to see the problem.
Bruno Feijão
Courses Plus Student 4,345 PointsYeah might as well do that, since we're getting no help here in the forums. Thx for the help though Don.
Hampton Paulk
5,093 PointsHello 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
19,488 PointsThank 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.
Bruno Feijão
Courses Plus Student 4,345 PointsThx 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
19,084 PointsHi, 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.