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

Rewrite Rule backslash

How do you add a backslash to the RewriteRule when your using another folder inside htdocs as your root folder instead of just using htdocs?

5 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi robertbobb,

Do you mean how do you go back a level if you're in a sub folder compared to just been in your htdocs folder? If so the simplest way is to just use ../ which tells the rule to go back one directory above itself.

Hi Chris, thanks for replying. Here is what I'm talking about:

Randy Hoyt uses the htdocs folder for his main site root. I'm not. I have another folder inside the htdocs folder which I use for my main site root:

All the RewriteRules work for me accept this one that should add the ending backslash to the url: RewriteRule ^(shirts/[0-9]+)$ /$1/ [R=301]

Here is the url to add the backslash to. Notice the difference between Randys url and mine. I'm using a site-folder for my main site root inside of htdocs:

localhost/shirts/108 <<<<================ Randys url

localhost/site-folder/shirts/108 <<<<================ My url

Here is the result to automatically add the ending backslash. It adds it but the site-folder is missing and I get this message =====>>>>> Object Not Found!

localhost/shirts/108/ <<==== Notice the site-folder is no longer there?

Please tell me this makes sense. Thanks

Chris Shaw
Chris Shaw
26,676 Points

The files in the htdocs folder aren't important to the server so you can delete them and work from that directory, as mentioned below however Apache offers a better solution which is a rule for .htaccess files called RewriteBase, see the below example.

RewriteBase /site-folder/

The reason we would use this instead of working in the root directory is it allows us to work from any directory on the server without disturbing the root website folder.

Hey Chris,

Wow! Thanks man. I did not know I could remove those files from htdocs. The only problem I had when using the RewriteBase is that the page 108 would not work. It kept giving me an error about object not found. But that's ok. I went on ahead and removed the files from htdocs and put all my file in there.

Really, great answer. I never would have thought of that. And yes, I also appreciated Ashlynn telling me about the RewriteBase too. But like I said, that still did not work. But putting my files directly into htdocs, everything works great!

Thank you both so very much.

God Bless.

Ashlynn Pai
Ashlynn Pai
11,679 Points

Had a hard time editing that. Trying again:

http://stackoverflow.com/questions/20159444/htaccess-is-not-working-in-xampp-in-windows

RewriteEngine On
RewriteBase /shirts4mike/

RewriteRule ^shirts/$ shirts/shirts.php [L]
RewriteRule ^shirts/([0-9]+)/$ shirts/shirt.php?id=$1 [L]
RewriteRule ^receipt.php$ receipt/ [R=301,L]
RewriteRule ^contact.php$ contact/ [R=301,L]
RewriteRule ^shirts.php$ shirts/ [R=301,L]
RewriteRule ^(shirts/[0-9]+)$ $1/ [R=301,L]

RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^shirt.php$ shirts/%1/? [R=301,L]

Hi Ashlynn,

Here is what's weird and what I found out. I just tried it again with the old way and the way you suggested and every number works (adds the backslash on the end) accept the last one ===>> 108.

I'm going to keep it the way you suggested, using the RewriteBase, but it still does not work for the last one ===>> 108.

What I'm really confused about is why Randy would use htdocs for his main site root. The files that are already in the htdocs as in i.e. ===>>xampp folder, index.html, index.php, forbidden folder, restricted folder, all the images, xampp favicon. How does he use htdocs with those files already in there? You cannot delete them right?

Other tutorials that I have studied from say to make a folder inside of htdocs for your main-site root folder. So I hope you can see how very weird this is for me to know that Randy uses htdocs for his main root. Is that not odd to you?

saranyamoellers
saranyamoellers
31,695 Points

Hi Ashlynn, Thank you for your code. My tailing slash and redirect old web addresses work now! Just don't know [L] mean? if you could tell me? thanks again for you code. Saranya

Leigh Maher
Leigh Maher
21,830 Points

Thanks Ashlynn. This really helped clear things up for me.

Ashlynn Pai
Ashlynn Pai
11,679 Points

Do you mean the .htaccess folder is is in the htdocs folder and your project files are in a subfolder? Or do you mean the issue discussed here https://teamtreehouse.com/forum/stuck-on-adding-a-trailing-slash-code-challenge? Or something else?

Hi Ashlynn, thanks for replying. Here is what I'm talking about:

Randy Hoyt uses the htdocs folder for his main site root. I'm not. I have another folder inside the htdocs folder which I use for my main site root:

All the RewriteRules work for me accept this one that should add the ending backslash to the url: RewriteRule ^(shirts/[0-9]+)$ /$1/ [R=301]

Here is the url to add the backslash to. Notice the difference between Randys url and mine. I'm using a site-folder for my main site root inside of htdocs:

localhost/shirts/108 <<<<================ Randys url

localhost/site-folder/shirts/108 <<<<================ My url

Here is the result to automatically add the ending backslash. It adds it but the site-folder is missing and I get this message =====>>>>> Object Not Found!

localhost/shirts/108/ <<==== Notice the site-folder is no longer there?

Please tell me this makes sense. Thanks

Ashlynn Pai
Ashlynn Pai
11,679 Points

This is just a guess after a few google searches, but does it help to remove the / in front of $1:

Old: RewriteRule ^(shirts/[0-9]+)$ /$1/ [R=301]

New: RewriteRule ^(shirts/[0-9]+)$ $1/ [R=301]

Here is the discussion on Stack Overflow:

http://stackoverflow.com/questions/20159444/htaccess-is-not-working-in-xampp-in-windows

From Stack Overflow:

The fix is having an relative url, instead of an absolute url. This can be done by removing the prefix /. For redirects, define RewriteBase with the subdirectory in it. I would add the [L] flag to every rule too. Your .htaccess should look something like this:

RewriteEngine On RewriteBase /shirts4mike/

RewriteRule ^shirts/$ shirts/shirts.php [L] RewriteRule ^shirts/([0-9]+)/$ shirts/shirt.php?id=$1 [L] RewriteRule ^receipt.php$ receipt/ [R=301,L] RewriteRule ^contact.php$ contact/ [R=301,L] RewriteRule ^shirts.php$ shirts/ [R=301,L] RewriteRule ^(shirts/[0-9]+)$ $1/ [R=301,L]

RewriteCond %{QUERY_STRING} ^id=([0-9]+)$ RewriteRule ^shirt.php$ shirts/%1/? [R=301,L]

Ashlynn Pai
Ashlynn Pai
11,679 Points

Hi Saranya, I just grabbed the code from Stack Overflow. Please check out http://httpd.apache.org/docs/2.4/rewrite/flags.html.

"The [L] flag causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, no further rules will be processed. This corresponds to the last command in Perl, or the break command in C. Use this flag to indicate that the current rule should be applied immediately without considering further rules."

saranyamoellers
saranyamoellers
31,695 Points

Thank you so much! Have a good day.