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

Tricia Martin
Tricia Martin
19,604 Points

Cleaning URLS with Rewrite Rules - Adding a Trailing Slash

My code works almost perfectly, but for some reason when I go to "localhost/shirts/108" it redirects to localhost/$ and I get this error "Not Found

The requested URL /$/ was not found on this server."

"localhost/shirts/108/" works, as does "localhost/shirts/101" - "localhost/shirts/107" (without the trailing slash). The only one that doesn't work is "localhost/shirts/108" without the trailing slash.

I don't know where I have gone wrong. I have even tried copying and pasting the code from the .htaccess file from the Project Files I downloaded and it does the same thing.

Here is the code I have:

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]

5 Answers

Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,702 Points

Hi Patricia, So I downloaded the project files for this section and threw them on my local web server to see if I could produce the same problem and I couldn't. The RewriteRules appear to match the one in the project file exactly.

You could try moving all the files within your web server over to a temp folder outside of the root directory or on desktop. Then re-download the entire project file and place those files in the proper directory. Then see if you can duplicate it with the project files. If it does NOT error out, then you can one by one copy paste a single file over from YOUR temp folder that you moved the original files to. Test the page after each file. When it errors out, you know which file has the error in it then you can pinpoint where you may have went wrong.

If it does error out, well, we will go from there. :)

Adam Moore
Adam Moore
15,825 Points

It may not be the only way but you can always define it twice with both cases RewriteRule ^shirts/$ RewriteRule ^shirts/$/

If that's what your asking for...

Tricia Martin
Tricia Martin
19,604 Points

I accidentally deleted all my own files (thought I was copying and pasting them somewhere else, inadvertently made shortcuts instead and deleted where they linked to).

Anyway, I downloaded the Project files and put them into the htdocs folder and I am still getting the same error. It seems like it must not be something wrong with the files but something else? It is so bizarre that it works with 101-107 perfectly but not with 108.

Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,702 Points

Oh no! Sorry about your files. :-(

This is definitely a very bizarre issue you are having. Since my project files ran just fine attempting the same steps. The only place I can think of starting with it is.

Which web server are you running & version of the web server?

Which web browser are you using to open it and which version?

Also, just to be sure we downloaded the same project files.

Did you download the project files from: Cleaning URLs with Rewrite Rules - Adding a Trailing Slash page ?

Tricia Martin
Tricia Martin
19,604 Points

I'm using MAMP Version 2.1.3 (2.1.3)

Chrome Version 28.0.1500.71

I downloaded the project files from Cleaning URLs with Rewrite Rules - Adding a Trailing Slash.

I continued onto the next section Adding Search: Controller and downloaded the project files from Creating the Search Page and it looks like it is working perfect now.

Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,702 Points

Ah, great to hear it fixed itself. I almost had to get my Google fingers out for this one.

Computer must of took a short processing vacation.

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Your browser might have cached the redirect. If you had some code that was incorrect, visited a URL, and been redirected, the browser can remember that the URL redirects. Even if you fix the code, the browser might still redirect for a while. I often test redirects like that in a different browser or in an incognito window. If it fixed itself like that, I would guess that was what was happening. Glad you got it working!