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

Tiffany McAllister
Tiffany McAllister
25,806 Points

Error 403 - PHP .htaccess redirect rules

Hi,

I'm currently doing the 'Enhancing a Simple PHP Application' course and have encountered an issue when using the [R] flag to redirect pages.

My .htaccess code is below:

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]

The first 2 rules work fine and the other rules work when I remove the [R] flag. When the redirect flag is included I get the error:

Access forbidden! You don't have permission to access the requested directory. There is either no index document or the directory is read-protected. If you think this is a server error, please contact the webmaster. Error 403

I am using XAMPP and my browser is Chrome.

Any help would be great :)

Try putting a / before the path that you're redirecting to. For example

RewriteRule ^receipt.php$ /receipt/ [R=301]

4 Answers

Tiffany McAllister
Tiffany McAllister
25,806 Points

Hi Ben,

Thanks for your reply.

That will not work because the site is not in the root of the htdocs folder. The site is in it's own subfolder 'shirts4mike' within the htdocs directory.

How about this. Assuming you want your .htaccess in the root of the htdocs folder and the site itself in the htdocs/shirts4mike folder, try this line in your .htaccess:

RewriteRule ^(shirts4mike)/receipt.php$ /$1/receipt/ [R=301]

This is working for me. Browser caching seems to sometimes make it look like it's not working when it is working correctly, or vice versa, so try clearing your cache too.

Alternatively, if you want your .htaccess inside of htdocs/shirts4mike folder, try this

RewriteRule ^receipt.php$ /shirts4mike/receipt/ [R=301]
Tiffany McAllister
Tiffany McAllister
25,806 Points

Thank you so much Ben! I used the second solution and it worked perfectly :)

I appreciate your help!

Thank you to both Tiffany and Ben for this. I've been struggling to get an answer for this for 2 days, and I saw this. It answered all my questions and provided answers and worked for me. Thanks for sharing.

Daniel Leak
Daniel Leak
4,105 Points

Seeing Ben's second solution helped, had already tried but seems browser caching is making me very unsure if I try to find a workaround. Just installed a 1 click browser extension that clears cache to make things a bit simpler

Daniel Leak
Daniel Leak
4,105 Points

Seeing Ben's second solution helped, had already tried but seems browser caching is making me very unsure if I try to find a workaround. Just installed a 1 click browser extension that clears cache to make things a bit simpler