在 CentOS 系統中,要自定義 Apache 錯誤頁面,請按照以下步驟操作:
sudo yum install httpd
sudo systemctl start httpd
sudo systemctl enable httpd
/usr/share/httpd/conf/templates/error
目錄下,你會找到一些默認的錯誤頁面模板文件(例如 error/404.html
)。你可以使用這些文件作為基礎,創建自己的自定義錯誤頁面。使用文本編輯器創建一個新的 HTML 文件,例如:sudo vi /usr/share/httpd/conf/templates/error/404.html
將以下內容粘貼到文件中,并根據需要進行修改:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>自定義 404 錯誤頁面</title>
</head>
<body>
<h1>抱歉,您訪問的頁面不存在!</h1>
<p>請檢查 URL 是否正確,或者返回 <a href="/">首頁</a>。</p>
</body>
</html>
保存并關閉文件。
重啟 Apache 服務以使更改生效:
sudo systemctl restart httpd
現在,當用戶訪問不存在的頁面時,將顯示您自定義的錯誤頁面。你可以為其他錯誤代碼(如 403、500 等)創建類似的文件,并在 /usr/share/httpd/conf/templates/error
目錄下進行自定義。