Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.