自訂 404 頁面
你可以輕鬆使用 Jekyll 提供自訂 404 錯誤頁面,以取代預設的錯誤 404 – 找不到檔案頁面,此頁面會在有人嘗試存取你網站上的失效連結時顯示。
在 GitHub Pages 上
任何位於_site
目錄根目錄的 404.html
都會由 GitHub Pages 和本機 WEBrick 開發伺服器自動提供。
只要在網站原始目錄的根目錄新增 404.md
或 404.html
,並包含前置資料,即可使用佈景主題的基本配置。
如果您計畫將檔案整理在子目錄中,錯誤頁面應設定下列 Front Matter 資料:permalink: /404.html
。這是為了確保編譯後的 404.html
位於已處理網站的根目錄,並由伺服器選取。
---
# example 404.md
layout: default
permalink: /404.html
---
# 404
Page not found! :(
在 Apache 網路伺服器上主機
Apache 網路伺服器會載入名為 .htaccess
的組態檔,以修改這些伺服器的功能。
只要將下列內容新增到 .htaccess
檔案中即可。
ErrorDocument 404 /404.html
使用 .htaccess
檔案,您可以自由地將錯誤頁面置於子目錄中。
ErrorDocument 404 /error_pages/404.html
其中路徑是相對於您網站網域的。
更多關於設定 Apache 錯誤頁面的資訊,請參閱 官方文件。
在 Nginx 伺服器上主機
程序與設定 Apache 伺服器一樣簡單,但略有不同。
nginx 組態檔取決於安裝它的系統。在大部分系統中,它是 nginx.conf
檔案,通常位於 /etc/nginx/
或 /etc/nginx/conf/
中。然而,在其他系統(例如 Ubuntu)中,您必須尋找包含伺服器相關資訊的 default
nginx 組態檔,它通常位於 /etc/nginx/sites-available/
或 /etc/nginx/sites-enabled/
中。將下列內容新增到您的 nginx 組態檔,即 nginx.conf
檔案或 default
檔案中
server {
error_page 404 /404.html;
location = /404.html {
internal;
}
}
如果 server
區塊已存在,請僅加入上方提供的 server
區塊內的程式碼。 location
指令可防止使用者直接瀏覽 404.html 頁面。
可在 nginx 官方文件 中找到更多有關 nginx 錯誤頁面的資訊。
編輯組態檔時請小心謹慎。