Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> 電腦軟體教學 >> 伺服器技術 >> 關於伺服器

Linux Apache 啟動報錯 Internal Server Error:mod_rewrite 模組未載入解決方案

編輯:關於伺服器

Apache 啟動或存取頁面時出現 Internal Server Error,錯誤日誌提示 Invalid command 'RewriteEngine' 或 RewriteRule,主因是 mod_rewrite 模組未載入。本文提供快速啟用步驟與驗證方法。

錯誤現象

The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator...
More information about this error may be available in the server error log.

錯誤日誌(/var/log/httpd/error_log 或 /var/log/apache2/error.log)典型內容:

[alert] Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration

解決方案:載入 mod_rewrite 模組

方法一:編輯主設定檔新增 LoadModule 指令

# CentOS/RHEL/Fedora: /etc/httpd/conf/httpd.conf
# Debian/Ubuntu: /etc/apache2/apache2.conf 或 /etc/apache2/mods-available/rewrite.load

LoadModule rewrite_module modules/mod_rewrite.so

方法二:取消註解既有 LoadModule 行(推薦)

# 找到以下行,移除前方 # 號
#LoadModule rewrite_module modules/mod_rewrite.so
→
LoadModule rewrite_module modules/mod_rewrite.so

方法三:Debian/Ubuntu 專用指令啟用

a2enmod rewrite
systemctl restart apache2

驗證模組已載入

# 查看已載入模組
httpd -M | grep rewrite
# 或
apache2ctl -M | grep rewrite

# 輸出應包含:
rewrite_module (shared)

測試 .htaccess Rewrite 規則生效

  1. 確認目錄允許覆寫:AllowOverride All(或至少 AllowOverride FileInfo)。
  2. 建立測試 .htaccess:
RewriteEngine On
RewriteRule ^test\.html$ /index.html [R=302,L]
  1. 瀏覽 https://your-domain/test.html 應重導向至 index.html。

常見衍生問題

現象原因對策
重寫規則不生效、無錯誤AllowOverride 設為 None修改虛擬主機目錄區塊 AllowOverride All
重寫迴圈 (Too many redirects)規則邏輯錯誤、缺少 L 旗標、條件不當啟用 RewriteLogLevel 3 偵錯、檢查規則順序
啟用後 Apache 無法啟動mod_rewrite 相依模組未載入、語法錯誤執行 httpd -t / apache2ctl configtest 檢查語法

結語

mod_rewrite 是 Apache 最常用也最易被遺漏的模組。部署新環境、遷移專案時,請將 LoadModule rewrite_module modules/mod_rewrite.so 納入標準檢查清單,並善用 a2enmod / httpd -M 驗證,避免上線後遭遇 500 錯誤。

Copyright © Windows教程網 All Rights Reserved