![無法載入 mod_rewrite Apache 模組](https://rvso.com/image/697384/%E7%84%A1%E6%B3%95%E8%BC%89%E5%85%A5%20mod_rewrite%20Apache%20%E6%A8%A1%E7%B5%84.png)
我正在嘗試使用mod_rewrite
的模組Apache24 server
,但無法加載它。我知道關於這個主題有很多問題,我已經解決了所有問題,但似乎沒有任何效果。這些是我到目前為止所遵循的步驟——
CHANGED
httpd.conf
檔案進行了以下更改
:未註釋LoadModule rewrite_module modules/mod_rewrite.so
B.AllowOverride None
變成AllowOverride All
重新啟動apache伺服器
使用命令提示字元 command 檢查載入的模組
httpd -M
。我可以看到 mod_rewrite 模組已載入。我附上下面的圖。
但是執行完所有這些步驟後,我看不到 mod_rewrite 作為載入的模組phpinfo
。
從上圖可以看出,沒有 mod_rewrite 載入模組。另外,作為一個瘋狂的駭客,我甚至嘗試使用.htaccess
文件重寫 URL,但這不起作用。.htaccess
儘管我已將該檔案放入我的根目錄中,但Apache 似乎忽略了該檔案。
Note: I am running `PHP` as an apache module
Using `WAMP` stack
Using `localhost` as server
我非常需要這個模組來實作 URL 重寫。你們能建議一些其他方式來加載這個模組嗎?
編輯
我嘗試從虛擬主機重寫 URL,因為答案表明模組已加載,而且它既不依賴.htaccess
也不依賴info.php
。我添加Virtual host
下面的設定---
<VirtualHost *:80>
<Directory "/Apache24/htdocs">
Options FollowSymLinks
AllowOverride All
DirectoryIndex index.html index.php
</Directory>
ServerName localhost
DocumentRoot "/Apache24/htdocs"
ErrorLog "/Apache24/logs/error.log"
CustomLog "/Apache24/logs/access.log" combined
<directory "/Apache24/htdocs">
<IfModule rewrite_module>
Options +FollowSymlinks
RewriteEngine On
</IfModule>
<IfModule rewrite_module>
RewriteRule ^working.php fun.html
</IfModule>
</directory>
# Rewrite Rules #####################
RewriteEngine On
RewriteRule ^working.php fun.html
# end Rewrite Rules #################
</VirtualHost>
working.php
當我嘗試運行時,上面的程式碼不會將其重定向到fun.html
。它只是說:
在此伺服器上找不到請求的 URL /working.php。
答案1
- 你不需要 .htaccess 或 AllowOverride 來讓 mod_rewrite 工作,不要使用,因為你是伺服器的管理員,因為它只會讓你的生活變得複雜,並為你的伺服器帶來不必要的開銷。
除了載入模組之外,您唯一需要的指令是:
重寫引擎開啟
額外的:
- 如果您需要的話,請考慮使用 Redirect/RedirectMatch (來自 mod_alias)來進行簡單的重定向。
- 考慮在虛擬主機上下文中定義重定向/重寫,以避免在不久的將來發生噩夢和脫髮。
答案2
從連結的螢幕截圖來看,您的伺服器根(檔案系統路徑)似乎是,但您在任何伺服器設定指令中C:/Apache24
都沒有引用?C:
您需要引用完整的檔案系統路徑,例如:
<Directory "C:/Apache24/htdocs">
RewriteRule ^working.php fun.html
該指令直接在 VirtualHost(<Directory>
容器外部)中使用,永遠不會匹配。當在 VirtualHost(或伺服器配置)上下文中使用時,mod_rewrite 會匹配完整的 URL 路徑,特別是包括斜線前綴。這是與在目錄/.htaccess
上下文中使用 mod_rewrite 時(排除斜線前綴時)的一個重要區別。