Jump to content

How to show a 403 or 404 error

Recommended Posts

I have my web page in the usual document root /var/www/html folder.

Say, my root folder for my website is "mypage". That is, inside "/var/www/html/mypage", I have the index.php, and other files and folders.



/var/www/html/mypage/members/xxx.php etc

/var/www/html/mypage/pictures/111.jpg, etc.





I want to show access forbidden (403) or files not found (404) errors. How do I catch when a user enters one of the existing folders directly on the URL bar and display appropriate message? 

For example:

/var/www/html/mypage/ will show /var/www/html/mypage/index.php which is desired. Good.

/var/www/html/mypage/pictures .... I don't want them to access this folder directly, so I want to show no permission error.

/var/www/html/mypage/hogehoge ... I want to show file(s) not found error.

/var/www/html/mypage/some_existing_and_valid_file.php/hogehoge  I want to show file(s) not found error.

I want to edit httpd.conf.

All I know now is how to disable directory listing and disable or enable a specific folder.  I am trying to find a generic solution.

Working environment: CentOS 7, PHP 5.6

Someone with knowledge and experience, please give me some guidance.


Share this post

Link to post
Share on other sites

Use ErrorDocument to set an error page for each code you want a custom error page.

If you disable directory listings (indexes) then /mypage/pictures will 403. /mypage/hogehoge will already 404. /mypage/index.php/hogehoge will go through index.php so you would need your code to show the 404 page.

Don't put this in httpd.conf. That's the configuration for the whole server. Put it in your site/VirtualHost configuration.

Share this post

Link to post
Share on other sites

Requinix: Thank you for the comments.

I put a line in .htaccess and now I can display error message.
I put: ErrorDocument 404 /err404.php, and created an error file with this content:

header("HTTP/1.1 404 Not Found");
header("Cache-Control: no-cache, must-revalidate");
header("Content-type: text/html; charset=UTF-8");
echo '<!DOCTYPE html><title>404 Not Found</title><p>Page Not Found.';


Only the  /mypage/index.php/hogehoge is giving me trouble.

I used Chrome's developer tool (F12), but  I can't see any 404 error under network, but under console, something "weird".

It says:

Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://localhost/mypage/index.php/css/styles.css"

Who said anything about the css? 

What do I write (probably at the very top of the index.php?) so that if someone enters *anything* that does not exist or make sense, show a 404.

BTW, if I type:


I do get a 404, which is nice ... but when I type  /mypage/index.php and even just an extra "/" ( /mypage/index.php/), I get /mypage/index.php page, but all my design is messed up.


By the way, my index.php begins like this:

<!DOCTYPE html>
<html lang="ja">
    <meta http-equiv="Content-Type" content="text/html; charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
    <title>some title</title>
    <link rel="stylesheet" href="./css/styles.css">


rest omitted.



Share this post

Link to post
Share on other sites

Before trying on the Centos server, I am now testing  with XAMPP on a windows machine.

Edited by x230

Share this post

Link to post
Share on other sites

Requinix and the good people of the Forum!

I have been able to avert this "crisis" with these lines in .htaccess:

<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteRule \.php/ - [R=404,L]

Happy holidays!

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.