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 Subfolders Introducing Constants

saranyamoellers
saranyamoellers
31,639 Points

receipt folder with index.php

Could anyone help me? I typed localhost/shirts4mike/receipt/ in my url I didn't get the thank you page to show up like Randy did. I only got the list of file in the receipt folder. i had to click the index.php in receipt folder to display the thank you page. How can I type localhost/shirts4mike/receipt/ to make it display index.php automatically? like Randy did.

8 Answers

saranyamoellers
saranyamoellers
31,639 Points
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]
Myroslav Tkachenko
Myroslav Tkachenko
10,581 Points

Please, specify what server do you use? In case it's Apache, check the settings for your vhost for something like this:

<Directory /yoursite>
    DirectoryIndex index.html index.php
</Directory>

or create .thaccess file in your site's root directory and add this line:

DirectoryIndex index.html index.php
saranyamoellers
saranyamoellers
31,639 Points

Hi Myroslav, I need to bother again.According to the video Redirecting Old Web Addresses in PHP I typed localhost/shirts4mike/receipt.php in the URL like Randy did My thank you page wouldn't show up I had Forbidden Error instead but If I typed localhost/shirts4mike/receipt/ my thank you page would work. I was wondering why I got the error ? do I need to fix something in the Apache config file?

Myroslav Tkachenko
Myroslav Tkachenko
10,581 Points

Saranya, first of all, check if your receipt.php exists and double check the URL you've typed. Maybe the URL for direct access should be localhost/shirts4mike/receipt/index.php If the URL is right and the file you're trying to access exists, then the issue may be with a file permissions.

Forbidden Errors (403) usually means that the permissions for the file doesn't allow a process to access it. Go to the directory where receipt.php file is (or index.php?), and type

chmod a+r receipt.php

or index.php instead receipt.php.

You can also check the 'receipt' directory permissions, it must be granted for execution for all. From your site root directory type

chmod a+x receipt

If this'll not help, please, provide your .htaccess file listing and exact paths to the files you're trying to access.

saranyamoellers
saranyamoellers
31,639 Points

Hello and thank you, I dont' have receipt.php but I have receipt folder and index.php(thank you page) inside it.

saranyamoellers
saranyamoellers
31,639 Points
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]
saranyamoellers
saranyamoellers
31,639 Points
<?php //DEFINE A CONSTANTS for links use BASE_URL,for include file use ROOT_PATH
define("BASE_URL","/shirts4mike/");
define("ROOT_PATH",$_SERVER["DOCUMENT_ROOT"]."/shirts4mike/"); //this is the sever path to homepage of website
Myroslav Tkachenko
Myroslav Tkachenko
10,581 Points

Try to add the slashes before your redirect paths in the rewrite rules like this:

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

not

receipt/ [R=301]

but

/receipt/ [R=301]

If this is right, then fix your other rules, they may be broken for the same reason

saranyamoellers
saranyamoellers
31,639 Points

Um, I already tried that. It didn't work. It did't redirect to the thank you page(receipt folder with index.php in the folder) but if I typed localhost/shirts4mike/receipt/ in URL , it would work.

Myroslav Tkachenko
Myroslav Tkachenko
10,581 Points

Also it may be a browser's cache issue, because of 301 redirect (as permanent) - the browser redirects you to the wrong path, which it stored in cache earlier. Fix your .htaccess as above and clear your browser's cache. You can check your redirects in Chrome Dev Tools / Network tab. You can clear the cache from there too from the context menu in this Network tab. UPD: or check 'Disable cache' checkbox in Chrome Dev Tools / Network tab

saranyamoellers
saranyamoellers
31,639 Points

It is getting warmer after I cleared the history in Chrome, I 've new error 404 Not Found good night :)

Myroslav Tkachenko
Myroslav Tkachenko
10,581 Points

I'm not sure that cleaning history alone will do, go to Developer Tools / Network tab and check 'Disable cache' checkbox there

saranyamoellers
saranyamoellers
31,639 Points

Thank you Myroslav, I added DirectoryIndex index.html index.php in my .htaccess file and it works! Yes , I use Apache. You save my day!

Stone Preston
Stone Preston
42,016 Points

do you have a file called index.php inside localhost/shirts4mike/receipt/ ? If not then thats why its not working.

saranyamoellers
saranyamoellers
31,639 Points

Hi Stone, Yes, I have index.php file inside receipt folder.

Stone Preston
Stone Preston
42,016 Points

hmm it should have worked then. try restarting your server and try again

saranyamoellers
saranyamoellers
31,639 Points

I did restart my server. That did't work. I had to type localhost/shirts4mike/receipt/index.php in URL to get my index.php to open up still. Thank you for your time.

saranyamoellers
saranyamoellers
31,639 Points

Hi guys, My redirect old web address and adding trailing slash work now. Thank you you guys for trying to help. Below is the code that works for me.