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

Suleyman Kolanci
PLUS
Suleyman Kolanci
Courses Plus Student 13,745 Points

Rewrite 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

You 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
Suleyman Kolanci
Courses Plus Student 13,745 Points

Thank 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?

The + (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/

Suleyman Kolanci
PLUS
Suleyman Kolanci
Courses Plus Student 13,745 Points

sorry 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

Are there any other rules that work on your home computer or it's just this one?

Suleyman Kolanci
PLUS
Suleyman Kolanci
Courses Plus Student 13,745 Points

it 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

The 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
PLUS
Suleyman Kolanci
Courses Plus Student 13,745 Points

sorry 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