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

varis darasirikul
varis darasirikul
10,832 Points

Troubles with Enhancing a Simple PHP Application Challenge task 1 about RewriteRule

In an earlier code challenge, we wrote a rewrite rule so that the web page [http://localhost/flavors/] executed the code at [all_flavors.php]. Unfortunately, this only works if the web address includes the trailing slash. If someone visits [http://localhost/flavors], they will see a 404: Page Not Found error. We'll need another rewrite rule to redirect from [/flavors] to [/flavors/]. Add a new line to the htaccess file below to accomplish this.

and I wrote this but it doesn't work and i dont know why.

RewriteEngine  On   
RewriteRule ^flavors/$ /all_flavors.php
RewriteRule ^all_flavors.php$ /flavors/%1/? [R=301]

this is link to a Challenge task (task 1)

http://teamtreehouse.com/library/enhancing-a-simple-php-application-2/cleaning-urls-with-rewrite-rules/adding-a-trailing-slash

3 Answers

Chris Dziewa
Chris Dziewa
17,781 Points

It looks like you need to replace the % sign with a $ sign in this last line: /flavors/%1/? . The percent sign would be used in relation to a RewriteCond rule that involves query strings. Also you shouldn't need the ? mark since that is also meant for that same type of rule and its function is to prevent the query string from being included in the url. The final product should be like this: /flavors/$1/ [R=301]
Hope this helped!

varis darasirikul
varis darasirikul
10,832 Points

I tried your answer but it seem to be doesn't work.

Chris Dziewa
Chris Dziewa
17,781 Points

Sorry, I just tried that one and realized what happened. You were actually adding too much. Since /flavors/ already references the php file. You only have to reference flavors in the regular expression. Your final line should look like this:

RewriteRule ^flavors$ /flavors/ [R=301]

All you are adding is the slash at the end of flavors.