简体   繁体   English

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

[英]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.

相关问题 重命名文件时,我的 mv 命令未显示此类文件或目录 - My mv command is showing no such file or directory when renaming a file Cpanel 没有让我选择更改我的一个域的根目录 - Cpanel is not giving me option to change the root directory for one of my domains 如何在笔记本电脑中安装whm,cPanel? - How to install whm,cPanel in my laptop? -bash: /usr/local/cpanel/3rdparty/bin/perl: 没有那个文件或目录 - linux 服务器崩溃了 - -bash: /usr/local/cpanel/3rdparty/bin/perl: No such file or directory - linux server kind of crashed 如果某个进程正在我的网页上运行,如何显示? - How can I display if a process is running on my web page? my_script抛出bash错误:/ home / usr / bin / my_sript:bin / bash /错误的解释器:没有这样的文件或目录。 我该如何纠正该错误? - my_script throwing an error of bash: /home/usr/bin/my_sript: bin/bash/ bad interpreter: No such file or directory. How can I correct this error? 如何防止linux杀死我的进程并生成核心文件 - how to prevent linux to kill my process and generate core file instead 命令清理我的Web服务器上的tmp目录 - command to clean tmp directory on my web server 如何将文件直接从另一台服务器传输到我的服务器 - how to transfer a file to my server directly from another server 如何找到“我的” lib目录? - How to find “my” lib directory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM