shared hosting 403 forbidden error
.htaccess | Configuring and Testing Custom Error Responses | 403 Forbidden example
Author: Dmytro Dzyubak
intro and outro music by Dmytro Dzyubak
Configuring and testing customized 403 Forbidden response using .htaccess file.
In this example we are going to use an .htaccess file located in the DocumentRoot where custom 403 error document or custom text to be displayed is configured. Additionally, our project has the “forbidden” directory which is used to test if our configuration actually works. The “forbidden” directory contains another .htaccess file and three dummy files with just some random data inside.
First we have to make sure that accessing the “forbidden/” directory triggers the 403 Forbidden error. This can be done by placing a specially configured .htaccess file inside the “forbidden/” directory. Do not confuse this .htaccess file with the one from the document root directory. So the .htaccess file located inside the “forbidden/” directory has Indexes turned off. This code ensures that directory listing is disabled and we won’t be able to see any of these files when accessing the “forbidden/” directory from the browser. Additionally, make sure that you do not have any index file inside this “forbidden” directory, otherwise this example won’t work and we will get 200 OK response instead. Now we are sure that accessing the “forbidden/” directory triggers the 403 Forbidden error. So let’s try it!
Let’s launch our browser. Now when we try to access the forbidden directory (I type “f-o-r-b-i-d-d-e-n”, slash and hit Enter) we get 403 Forbidden status code and “Forbidden. Directory listing has been disabled.” message. This message is taken from this string of an .htaccess file located in our web root.
Certainly, we could provide here a “/403.html” page instead. This just shows various possible ways of configuring Apache using the .htaccess file.
like / subscribe 🙂