繁体   English   中英

如何在我的 cpanel 服务器上显示错误页面而不是在我的目录中显示文件

[英]How to display error page instead of showing file in my directory on my cpanel server

当 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文件夹。 我希望它将用户重定向到错误页面,而不是显示此文件夹中的所有文件。 在此处输入图像描述

一种方法是在该文件夹内创建一个 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 页面与您处理 404 错误的任何页面)

我假设您也可以使用 .htaccess 中的一些正则表达式来做到这一点

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM