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

Evans B. Ofori
PLUS
Evans B. Ofori
Courses Plus Student 13,720 Points

Stuck on Adding A Trailing Slash code challenge

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.

Please any show light….

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

3 Answers

You are close, you do want to use [R=301], but the challenge is to redirect from /flavors to /flavors/ not redirect to /all_flavors.php. I won't provide the solution but take the code below as an example, assuming I want to redirect from /old/URL/path to /new/path/, my Rewrite Rule would be as follows:

RewriteRule ^old/URL/path$ /new/path/ [R=301]

Hopefully this helps solve the issue. If you have any questions on this let me know.

Evans B. Ofori
PLUS
Evans B. Ofori
Courses Plus Student 13,720 Points

hi Codie,

Thanks for your reply, but I still don't get it. If I understand you correctly I should not use /$1/ I have tried all these cole below. Pleas more hint if you can or any link I could read a little bit more into this.

RewriteRule ^flavors/all_flavors.php$ /flavors/ [R=301]

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

Evans,

Your 2nd example is very close:

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

The ^ begins the line to match and $ ends the line to match. So what the above is saying is redirect /flavors/ to /flavors/. You will need to change it so that it redirects /flavors to /flavors/. This knowledge document on Web Host MediaTemple does a good job explaining how the Regular Expressions work in .htaccess: Using .htaccess rewrite rules.

I hope this information is helpful. I'm trying to explain it as best I can without providing the exact answer.

Evans B. Ofori
PLUS
Evans B. Ofori
Courses Plus Student 13,720 Points

Hi Codie,

Now it works. Thanks a lot! I really appreciate your help and the link.