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
Suleyman Kolanci
Courses Plus Student 13,745 PointsRewrite with RegEx not working
I am trying to get second Rewrite rule on PHP tutorials
RewriteRule ^shirts/$ shirts/shirts.php
works but
RewriteRule ^shirts/([0-9]+)/$ shirts/shirt.php?id=$1
returns not found please help me find a solution thanks
4 Answers
Robert Bojor
Courses Plus Student 29,439 PointsYou can either do it like
RewriteRule ^shirts/([0-9]{1,3})/$ shirts/shirt.php?id=$1
meaning you are expecting 1 to 3 numbers between 0 and 9 or you can just go and match whatever it comes there like so
RewriteRule ^shirts/([0-9]*)/$ shirts/shirt.php?id=$1
and will not matter how many numbers it finds, it will pass them to the $1 placeholder.
Suleyman Kolanci
Courses Plus Student 13,745 Pointssorry to bug you again Robert. I tried your code at my job computer worked perfectly but i came home and used it on my home computer i have the same problem saying object ot found what else can be the problem do you know?
Edit: i go from shirts link and works fine but if i type with my hand doesnt find it
Robert Bojor
Courses Plus Student 29,439 PointsAre there any other rules that work on your home computer or it's just this one?
Suleyman Kolanci
Courses Plus Student 13,745 Pointsit works with slah(/) at the end but i cant do "shirts/108" i have to do "shirts/108/ and my code is like this:
RewriteRule ^shirts/(0-9)*/$ /shirts/shirt.php?id=$1
Robert Bojor
Courses Plus Student 29,439 PointsThe rule should actually be:
RewriteRule ^shirts/([0-9]*)$ /shirts/shirt.php?id=$1
instead of what you have. You forgot the square brackets [ ] that mark the range of the expected characters.
Suleyman Kolanci
Courses Plus Student 13,745 Pointssorry it was my typo i had brackets. i did what you did . i took out the last forward slash it says not found but it works at my work computer. its gotta be my apache
Suleyman Kolanci
Courses Plus Student 13,745 PointsSuleyman Kolanci
Courses Plus Student 13,745 PointsThank you very much Robert both way it works just fine. However i dont understand why it didnt pass it the way randy did did. must be version differences right?
Robert Bojor
Courses Plus Student 29,439 PointsRobert Bojor
Courses Plus Student 29,439 PointsThe + (plus sign) used in the rewrite rule means that one or more of that kind, in our case 0 through 9 are expected.
Frankly, I don't know why it didn't work like Randy used it, since obviously for him it worked, but I usually prefer to be very specific with the rules when it comes to URLs. I tend not to write the exact code that the teacher writes and go a bit on the edge and experiment with the code.
Here's a nice cheat sheet for rewrite rules in case you need it in the future: http://www.cheatography.com/davechild/cheat-sheets/mod-rewrite/