Apache Rewrite Engine 將所有請求重定向到index.php

Apache Rewrite Engine 將所有請求重定向到index.php

我在 Apache Web 伺服器上有一個 PHP Web 應用程式。

伺服器上 Web 應用程式的路徑是:/var/www/html/intern/organisation/example_app

Web 應用程式的 URL 是:
https://www.example.de/org1/intern/example_app/

我希望所有請求(例如)https://www.example.de/org1/intern/example_app/view1/都重定向到

https://www.example.de/org1/intern/example_app/index.php

在我的本機 XAMPP 伺服器上,我使用以下 .htaccess 文件,該文件適用於此目的:

RewriteEngine On
RewriteBase /example_app
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [L]

但我不知道該文件在伺服器上應該是什麼樣子?我還在伺服器上使用httpd.conf。我想知道,該文件是否幹擾了.htaccess下面的文件:/var/www/html/intern/organisation/example_app/

有誰知道如何在伺服器上配置重寫?

答案1

.htaccess子目錄中的檔案應/org1/intern/example_app/如下所示:

DirectoryIndex index.php

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

然後,您可以.htaccess在本地和即時伺服器上使用相同的文件,前提是它位於example_app/子目錄中。

如果該.htaccess檔案與您要重寫的檔案位於同一目錄中,index.php則不需要該RewriteBase指令。

DirectoryIndex指令可能已在伺服器配置中設置index.php,因此此處可能不需要它,但為了完整性,應將其包含在內。當請求應用程式目錄本身時(即https://www.example.de/org1/intern/example_app/),它是必需的。

請注意,這是“內部重定向”,或更通常稱為“重寫”。如果您簡單地說“重定向”,那麼這意味著“外部(HTTP)重定向”,這完全是另一回事。


我想知道,該文件是否會幹擾.htaccess file下面的 :/var/www/html/intern/organisation/example_app/

多個.htaccess檔案(和伺服器配置)可能會發生衝突。但是,子配置中的 mod_rewrite 指令(在相同的情境, IE。目錄)將完全覆蓋父配置中的 mod_rewrite 指令(預設)。


在旁邊:(或可能不是)

伺服器上 Web 應用程式的路徑是:/var/www/html/intern/organisation/example_app

Web 應用程式的 URL 是:https://www.example.de/org1/intern/example_app/

雖然有點令人困惑,但文件路徑和 URL 路徑似乎不匹配?您是否Alias在伺服器配置中進行了額外的重寫或配置,或者example_app檔案路徑中的目錄與example_appURL 路徑中的目錄不同?

答案2

非常感謝您的詳細解答。重寫規則不是問題。

問題很簡單。整個 .htaccess 沒有字,因為我必須在 httpd.conf 上啟動AllowOveride。

相關內容