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!

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 Enhancing a Simple PHP Application Cleaning URLs with Rewrite Rules Adding A Trailing Slash

Chris Collier
Chris Collier
17,774 Points

Why does this rewrite rule work (leading slash)?

Hi, I had to go to an earlier forum response to pass this objective. I know how to pass it now, but not why this works. Here's the question:

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

This is what we're given to start:

RewriteEngine  On
RewriteRule ^flavors/$ /all_flavors.php

I would think that the answer would be:

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

But it turns out that the answer is:

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

The difference there is that my proposed rewrite rules has a leading / in the first part. Why is this not correct? Why is it that it works if we search for the term without a slash and then replace it with a term with a slash? Wouldn't that cause a double-slash to be sent to the browser? Thanks in advance.

1 Answer

Hi Chris, great to see you at the Treehouse Forums.

Mmm, excellent question. I believe that little ^ symbol plays a role as announcing that you are now starting the beginning of a string.

See, when you write some of this stuff, if it's not escaped properly, it can be interpreted a number of ways.

An example:

Lets say you want to set a RewriteRule to match rss.xml

 RewriteRule    ^rss.xml$    rss.php    [NC,L]  

Well, thats fine, it will match rss.xml. But it will also match rss1xm "and rss-xml

To fix this, we escape the string. And because you announce it AS a string by using that little ^ bit at the beginning, it knows that what you are able to declare is a string TO escape bits from it. Like so:

 RewriteRule    ^rss\.xml$    rss.php    [NC,L] 

Here it goes ok, I know this is a string, you said ^ and I can see you are wanting to escape the dot with a backslash. So I now know to include it in and only match rss.xml, not rss.xml and rss1xm "and rss-xml

Bear in mind, this only is useable for the pattern. Not the substitution

^rss.xml$ is the pattern rss.php is the substitution

There are many other character symbols that can be used for the pattern you want it to examine, ^ is just one of them

. (any character)
* (zero of more of the preceding)
+ (one or more of the preceding)
{} (minimum to maximum quantifier)
? (ungreedy modifier)
! (at start of string means "negative pattern")
^ (start of string, or "negative" if at the start of a range)
$ (end of string)
[] (match any of contents)
- (range if used between square brackets)
() (group, back referenced group)
| (alternative, or)
\ (the escape character itself)

So in your instance, you are asking it to match a string not necessarily it's directory division with a forward slash.

^ is the start of your string, you want to search the word itself only, you tell it that the string ends with a $ and it is with your substitution section where you are specifying the url to go to.

I hope this sheds some light on the subject

Happy coding!