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

Joan Valls de Padrines Amengual
Joan Valls de Padrines Amengual
11,760 Points

RewriteRules in subfolder

Hi,

I'm stuck applying the rewriterules.

I have all of them exactly as Randy has but i work on a subfolder in my localhost (/th-php).

The problem happens when i try to access an old URL, if i submit localhost/th-php/shirts.php it redirects me to localhost/shirts/

I tried to figure out where to set my subfolder name in order to rewriterules to work properly but no success so far.

The htaccess file is sitting inside the /th-php/ subfolder in my localhost.

The content of my htaccess:

RewriteEngine On

RewriteRule ^shirts/$ shirts/shirts.php
RewriteRule ^shirts/([0-9]+)/$ shirts/shirt.php?id=$1

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

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

Any tip?

2 Answers

Joan Valls de Padrines Amengual
Joan Valls de Padrines Amengual
11,760 Points

Finally I had to move the project to the main folder. This way all the Rewrites works as expected. Tried on a shared hosting and works perfectly with a subdomain pointing to the folder where the page is.

But I still have the doubt of how to establish a rewriterule that keeps the subfolder. I thought the RewriteBase adds this but didn't worked as expected.

Anyway, It worked fine just as said in the vΓ­deos but I have to work on the main folder in may local server and the shared hosting. I'm using XAMMP btw, and I will need to change the configuration of the main directory as I did in the shared hosting.

If anyone know how to apply the subfolder to the rewriterules I'm still trying to figure out.

RewriteBase /
RewriteEngine On

RewriteRule ^shirts/$ shirts/shirts.php
RewriteRule ^shirts/([0-9]+)/$ shirts/shirt.php?id=$1

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

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

Unless I'm mistaken, I believe these lines:

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

will add a trailing slash to the end of your url. Maybe you could add a similar rule to prefix the query string with your subfolder.

There may be a simpler method, but it depends on your local server software. I you're using MAMP for instance, you can very easily change the home directory.

Also, have you started on ROOT_PATH and BASE_URL at this point?