簡體   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