要自定義Ubuntu Apache2錯誤頁面,請按照以下步驟操作:
sudo apt update
sudo apt install apache2
/var/www/html
目錄下創建一個名為custom-error-pages
的目錄:sudo mkdir /var/www/html/custom-error-pages
404.html
)上傳到新創建的目錄中??梢允褂肍TP、SCP或其他文件傳輸方法。確保文件具有適當的權限,以便Apache2可以訪問它們:sudo chmod 644 /var/www/html/custom-error-pages/404.html
/etc/apache2/sites-available/000-default.conf
:sudo nano /etc/apache2/sites-available/000-default.conf
<VirtualHost>
標簽內,找到ErrorDocument
指令。如果沒有找到,請在<Directory>
標簽內添加它。將自定義錯誤頁面與相應的HTTP狀態代碼關聯起來。例如,要將自定義404錯誤頁面與狀態代碼404關聯,請添加以下行:ErrorDocument 404 /custom-error-pages/404.html
如果要為其他狀態代碼添加自定義錯誤頁面,請使用相同的方法,將狀態代碼替換為所需的狀態代碼。
保存并關閉配置文件。
重新啟動Apache2以使更改生效:
sudo systemctl restart apache2
現在,當用戶訪問不存在的頁面時,Apache2將顯示自定義的404錯誤頁面。您可以為其他HTTP狀態代碼添加自定義錯誤頁面,只需遵循相同的步驟并更改狀態代碼即可。