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 Introducing Rewrite Rules

David Kim
David Kim
4,498 Points

.htaccess is not working for netbeans. My directory is "htdocs/tshirt_store/.htaccess"

I have created the .htaccess and enabled netbeans to view .htaccess files. Also, added the code here:

RewriteEngine On

RewriteRule ^shirts/$ /shirts/shirts.php

I am not getting errors but even with this modification going to /tshirt_store/shirts/ takes me to the index page and not the shirts.php file.

Igor Prymak
Igor Prymak
13,590 Points

Try removing / in RewriteRule before shirts/shirts.php. It worked for me. So it is: RewriteRule ^shirts/$ shirts/shirts.php

3 Answers

If htdocs folder is the DocumentRoot on your Apache server setup than you need to set /tshirt_store as your base apth in .htaccess (or set htdocs/tshirt_store as your DocumentRoot in Apache).

RewriteEngine On
RewriteBase /tshirt_store
RewriteRule ^shirts/$ /shirts/shirts.php

Your current setup is converting url's like /shirts/anything to /shirts/shirts.php but what you actually want is to convert it to /tshirt_store/shirts/shirts.php

Becky Christofferson
Becky Christofferson
15,047 Points

You are officially my hero. I've been staring at this so long that I'm not sure Shirt is even a word.

Daniel Quaidoo
Daniel Quaidoo
23,937 Points

Can someone help me with this having the same issue but doesn't seem to work. Tried the solution above

Robert Mews
Robert Mews
11,540 Points

I'm using a live Apache server and having terrible issues with 403 Forbidden errors trying to access mydomain.com/shirts in the browser. My .htaccess was, by default, already created with the following command in it: SetEnv DEFAULT_PHP_VERSION 54. The .htaccess file was also not in the same location as in the video - it is located one directory outside alongside the htdocs folder, or in my case the public_html folder.

Here is the first code base I tried:

SetEnv DEFAULT_PHP_VERSION 54
RewriteEngine On
RewriteRule ^shirts/$ shirts/shirts.php

I then tried this:

SetEnv DEFAULT_PHP_VERSION 54
RewriteEngine On
RewriteBase /public_html
RewriteRule ^shirts/$ shirts/shirts.php

And even tried this:

SetEnv DEFAULT_PHP_VERSION 54
RewriteEngine On
RewriteBase /public_html
RewriteRule ^shirts/$ /shirts/shirts.php

It seems my readwrite rules need to be configured? But I can't figure out how to access those rule using my CPanel?