Jump to content

How to show a 403 or 404 error


x230

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.

Example:

/var/www/html/mypage/css/style.css

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

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

/var/www/html/mypage/index.php

/var/www/html/mypage/menu.php

/var/www/html/mypage/login.php

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.

Thanks!

Link to comment
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.

Link to comment
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:

<?php
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.';
exit;
?>

 

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:

 /mypage/index.phpxx

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">
<head>
    <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">
</head>
<body>

...
...

rest omitted.

 

 

Link to comment
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]
</IfModule>

Happy holidays!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.