無法載入 mod_rewrite Apache 模組

無法載入 mod_rewrite Apache 模組

我正在嘗試使用mod_rewrite的模組Apache24 server,但無法加載它。我知道關於這個主題有很多問題,我已經解決了所有問題,但似乎沒有任何效果。這些是我到目前為止所遵循的步驟——

  1. CHANGEDhttpd.conf檔案進行了以下更改
    :未註釋LoadModule rewrite_module modules/mod_rewrite.so
    B.AllowOverride None變成AllowOverride All

  2. 重新啟動apache伺服器

  3. 使用命令提示字元 command 檢查載入的模組httpd -M。我可以看到 mod_rewrite 模組已載入。我附上下面的圖。

我的命令提示字元的螢幕截圖

但是執行完所有這些步驟後,我看不到 mod_rewrite 作為載入的模組phpinfo

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

  1. 你不需要 .htaccess 或 AllowOverride 來讓 mod_rewrite 工作,不要使用,因為你是伺服器的管理員,因為它只會讓你的生活變得複雜,並為你的伺服器帶來不必要的開銷。
  2. 除了載入模組之外,您唯一需要的指令是:

    重寫引擎開啟

額外的:

  1. 如果您需要的話,請考慮使用 Redirect/RedirectMatch (來自 mod_alias)來進行簡單的重定向。
  2. 考慮在虛擬主機上下文中定義重定向/重寫,以避免在不久的將來發生噩夢和脫髮。

答案2

從連結的螢幕截圖來看,您的伺服器根(檔案系統路徑)似乎是,但您在任何伺服器設定指令中C:/Apache24都沒有引用?C:您需要引用完整的檔案系統路徑,例如:

<Directory "C:/Apache24/htdocs">
RewriteRule   ^working.php   fun.html

該指令直接在 VirtualHost(<Directory>容器外部)中使用,永遠不會匹配。當在 VirtualHost(或伺服器配置)上下文中使用時,mod_rewrite 會匹配完整的 URL 路徑,特別是包括斜線前綴。這是與在目錄/.htaccess上下文中使用 mod_rewrite 時(排除斜線前綴時)的一個重要區別。

相關內容