Apache2 mod_rewrite 似乎沒有做任何事情

Apache2 mod_rewrite 似乎沒有做任何事情

我很難讓 mod_rewrite 在 Debian 10 上的 Apache2 中運作。

a2enmod rewrite
systemctl restart apache2

並且沒有錯誤並且可以看到該模組

apachectl -M
...
rewrite_module (shared)
...

儘管當我將其新增至可用網站中的虛擬主機時

<VirtualHost *:80>
   ServerName default.nothing
   ServerAlias www.default.nothing
   DocumentRoot /var/www/html/public_html/00-default
   <Directory "/volume/dev/html/public_html/00-default">
      Options Indexes FollowSymLinks MultiViews
      AllowOverride All
      Order allow,deny
      Allow from all
      RewriteEngine on
      RewriteRule ^192.168.20.87$ nothing
   </Directory>
   <IfModule mpm_user_module>
      ServerEnvironment apache apache
   </IfModule>
</VirtualHost>

希望能重寫urlhttp://192.168.20.87/page.php如瀏覽器標籤中的 http://nothing/page.php。無論我在 RewriteRule 中輸入什麼,似乎都沒有發生。我確信我正在做某事

答案1

RewriteEngine on
RewriteRule ^192.168.20.87$ nothing

出於某種原因,如果我使用 .htaccess 進行操作,它會起作用

http://192.168.20.87/page.php儘管即使你“do it with ”,這也不會匹配URL .htaccess,所以你必須做別的事

上述規則(在<Directory>容器中使用時)與表單的 URL 相符http://192.168.20.87/192.168.20.87(或http://default.nothing/192.168.20.87- 如果您的主機名稱解析)。

RewriteRule 圖案僅符合 URL 路徑,不符合主機名稱(即192.168.20.87)。因此,這與/page.php, ( 或page.php- 相對路徑/無斜線前綴 - 當在目錄上下文如<Directory>.htaccess

所以這需要像下面這樣:

RewriteRule ^page\.php$ nothing

(雖然不清楚你在這裡想做什麼,但什麼是「無」?如果你試圖觸發 404 那麼這並不是真正的方法。)

正如 @Gerrit 在評論中鬆散地提到的,當該RewriteRule指令用於伺服器或者虛擬主機上下文(即不在<Directory>.htaccess容器中 - a目錄context),那麼該指令匹配的 URL 路徑RewriteRule是相對於根目錄的,以斜杠開頭,因為該指令在映射到檔案系統之前就被處理得更早。例如。^/page\.php$


更新:

DocumentRoot /var/www/html/public_html/00-default
<Directory "/volume/dev/html/public_html/00-default">

我剛剛注意到您的DocumentRoot<Directory>指令引用了兩個不同的檔案系統位置?由於問題中的資訊有限,這個<Directory>容器永遠不會被處理。

但是,除非您在其他地方有其他指令(我假設您必須有),否則.htaccess放置在文檔根目錄(我假設您放置它的位置)中的任何文件也永遠不會被處理。

相關內容