[英]How to display error page instead of showing file in my directory on my cpanel server
Can anyone help with how to display the error page instead of showing the directory for my files when no file is pointed to on the URL?当 URL 上没有文件指向时,谁能帮助如何显示错误页面而不是显示我的文件的目录? For instance, in the attached picture I simply remove thefile.php and input the URL on a browser like www.mywebsite.com/foldername/ instead of www.mywebsite.com/foldername/thefile.php and it displays all the files under this folder.
For instance, in the attached picture I simply remove thefile.php and input the URL on a browser like www.mywebsite.com/foldername/ instead of www.mywebsite.com/foldername/thefile.php and it displays all the files under this文件夹。 I want it to redirect the user to the error page instead of showing all files in this folder.
我希望它将用户重定向到错误页面,而不是显示此文件夹中的所有文件。
One way to to it would be to create an index.php file inside that folder with the content bellow:一种方法是在该文件夹内创建一个 index.php 文件,其内容如下:
<?php
$dir = "."; // the directory you want to check
$exclude = array(".", "..", "index.php"); // ignore . .. and file itself
$files = scandir($dir);
$files = array_diff($files, $exclude); // delete the entries in exclude array from your files array
if(!empty($files)) {
echo "there are files";
}
else
{
header("Location: /404.php");
}
?>
When you access http://url/folder, index.php is the default php file loaded, so if there is nothing else in the folder except for the index.php file, it will show the 404 page (you have to replace the /404.php page with whatever page you have for handling 404 errors) When you access http://url/folder, index.php is the default php file loaded, so if there is nothing else in the folder except for the index.php file, it will show the 404 page (you have to replace the /404.php 页面与您处理 404 错误的任何页面)
I assume you could do it with some regex in .htaccess as well我假设您也可以使用 .htaccess 中的一些正则表达式来做到这一点
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.