x230 Posted December 22, 2018 Share Posted December 22, 2018 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! Quote Link to comment Share on other sites More sharing options...
requinix Posted December 22, 2018 Share Posted December 22, 2018 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. Quote Link to comment Share on other sites More sharing options...
x230 Posted December 23, 2018 Author Share Posted December 23, 2018 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. Quote Link to comment Share on other sites More sharing options...
x230 Posted December 23, 2018 Author Share Posted December 23, 2018 (edited) Before trying on the Centos server, I am now testing with XAMPP on a windows machine. Edited December 23, 2018 by x230 Quote Link to comment Share on other sites More sharing options...
x230 Posted December 25, 2018 Author Share Posted December 25, 2018 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.