indx.php從 URL 中洗掉;總是??/從??/index.php
...包括子目錄,它們可能有自己的 .htaccess 和 index.php。
我將 Apache 與 .htaccess 一起使用。
我在域的子目錄中有一個帶有 index.php 的 webapp,在這里說:
example.tld/somedir/
但是,它可以安裝到任何目錄,例如這些...
example.tld/anotherdir/
sub.domain.tld/
another.tld/
- 出于我們的目的,每個都有自己的 index.php 和自己的 .htaccess
- On
example.tld/somedir/:example.tld/,sub.example.tld/, et cetera 也有各自的 index.php 和 .htaccess,用于各自的目的 example.tld/dir/如果放置在WordPress 或 October 或 SuitCRM 安裝的子目錄中,我需要它仍然可以作業example.tld/。
始終index.php通過 .htaccess隱藏
我想確保:
- 以上四個/(根)地址都鏈接到同一目錄中的 index.php。
- index.php 總是重定向到/ (root)
- 例子
example.tld/anotherdir/index.php -> example.tld/anotherdir/
sub.domain.tld/index.php -> sub.domain.tld/
another.tld/index.php -> another.tld/
我需要這樣的東西
RewriteRule ^.*$ /{$PWD}/index.php [L,QSA]
...以及必須在它之前的任何 Rewrite 陳述句。
這些問題不提供答案:
- htaccess 將 index.php 重定向到根目錄(包括子域)
- 這不涉及子目錄,僅涉及子域
- 這重定向
example.tld/dir/index.php到example.tld/,但我需要example.tld/dir/
- htaccess 從 url 中洗掉 index.php
- 這只會洗掉 indx.php
example.com/index.php/dir/subdir - 這不會
example.com/dir/index.php像我需要的那樣洗掉 index.php
- 這只會洗掉 indx.php
- .htaccess 重定向 index.php
- 這從來沒有一個有效的答案
- 這重定向
example.tld/dir/index.php到example.tld/,但我需要example.tld/dir/
- 重定向到 .htaccess 目錄的 index.php
- 這需要指定
/dir/,我不能這樣做。 - 這重定向
example.tld/dir/index.php到example.tld/,但我需要example.tld/dir/ - 答案不適用于 OP。
- 這需要指定
- htaccess 洗掉 index.php 和 index.php/
- 這也會從 URL 中洗掉子目錄。
- 使用 htaccess 將所有重定向到 index.php
- 這也需要指定
/dir/在example.tld/dir/index.php - 答案是:
RewriteRule ^.*$ /mvc/index.php [L,QSA],我不能使用。
- 這也需要指定
- .htaccess - 簡單重定向:索引(沒有擴展名!)/index.html/index.php 到根目錄
- 不解決我需要的子目錄。
- 使用 .htaccess 從 URLS 中洗掉 index.php
- 這重定向
example.tld/dir/index.php到example.tld/,但我需要example.tld/dir/
- 這重定向
uj5u.com熱心網友回復:
要從index.php請求的目錄中提供服務,您可以使用 mod_dir 的DirectoryIndex指令(可能已經在服務器配置中設定,盡管默認為index.htmlonly) - 您不需要 mod_rewrite。例如:
# Serve "index.php" from the requested directory
DirectoryIndex index.php
這指示 Apacheindex.php從請求的任何目錄提供服務。例如。/foo/bar/然后/foo/bar/index.php通過內部子請求(無重定向)提供請求。如果index.php該目錄中不存在,您將收到 403 Forbidden 回應(假設目錄串列 - 由 mod_autoindex 生成 - 已禁用)。
要從直接請求的任何 URL 中洗掉,您可以使用 mod_rewrite 。 index.php例如:
RewriteEngine On
# Remove "index.php" from any URL and redirect back to the "directory"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(. /)?index\.php(/|$) /$1 [R=301,L]
以上將重定向如下,保留請求的協議和主機名:
/index.php到//foo/index.php到/foo//foo/bar/index.php到/foo/bar//fooindex.php- 無重定向(預期 404)/foo/index.php/bar(包含路徑資訊)到/foo/(路徑資訊已洗掉)/foo/index.phpbar- 無重定向(預期 404)
(可選)捕獲組(.*/)?包含 URL 路徑之前的部分index.php。然后可以使用反向參考在替換字串中使用它$1。在檔案根中請求時的情況下/index.php,這是空的。當存在子目錄時,它包含一個格式為 的字串subdir/,包括尾部斜杠。
如果您的.htaccess檔案中沒有其他指令,那么您并不嚴格需要檢查環境變數的條件。REDIRECT_STATUS此條件可確保在您稍后在檔案中有可能將請求重寫為index.php.
如果檔案中確實有其他指令,那么順序可能很重要。index.php通過外部重定向洗掉的這條規則必須在任何現有的重寫之前,靠近檔案頂部。
請注意,index.php無論請求的 URL 是否實際映射到真實檔案,或者前面的 URL 路徑是否作為物理目錄存在,這都會從 URL 中洗掉。因此,無論是否是物理目錄,都會/<something>/index.php被重定向到。這種檢查可以以額外的檔案系統檢查為代價來實作——但它可能不是必需的。/<something>//<something>/
注意:首先使用 302(臨時)重定向進行測驗,以避免潛在的快取問題。只有在您測驗過它按預期作業后才更改為 301(永久)重定向。
更新#1:
這些問題不提供答案:
htaccess 將 index.php 重定向到根目錄(包括子域)
- 這不涉及子目錄,僅涉及子域
- 這重定向
example.tld/dir/index.php到example.tld/,但我需要example.tld/dir/
實際上,您鏈接到的第一個問題確實回答了您的問題,即index.php從請求的 URL 中洗掉。它確實解決了子目錄并會重定向example.tld/dir/index.php到example.tld/dir/(不像example.tld/你所說的那樣)。
討論子域的問題部分有點誤導,因為它與子域沒有任何關系。
鏈接問題中提出的解決方案基本上與我在上面所做的事情相同,只是它可以說匹配太多(而且不夠)。它會錯誤地/不必要地重定向/fooindex.php到/foo(沒有尾部斜杠),并且無法重定向包含路徑資訊的 URL(這可能是惡意的)。例如。/foo/index.php/bar將無法重定向,但仍會提供/foo/index.php(除非AcceptPathInfo已被禁用)的內容。盡管這些“錯誤”是否真的會在您的情況下引起問題是另一回事。
更新#2:
我的代碼完全在目錄中
example.tld/dir
上面的代碼假定.htaccess檔案位于檔案根目錄中。如果.htaccess檔案位于安裝應用程式的目錄中,則您需要像這樣修改上面的內容:
# Remove "index.php" from any URL and redirect back to the "directory"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(. /)?index\.php
RewriteRule (^|/)index\.php(/|$) /%1 [R=301,L]
%1反向參考(與 相對$1)指的是前面的 CondPattern 中捕獲的組。這自然包括完整的 URL 路徑,因此不必對.htaccess檔案所在的目錄進行硬編碼。
這適用于包含.htaccess檔案的目錄及其任何子目錄。請注意,默認情況下,這會完全覆寫父檔案中可能存在的任何 mod_rewrite 指令.htaccess(默認情況下不繼承 mod_rewrite 指令)。
...包括子目錄,它們可能有自己的 .htaccess和 index.php。
如果其他子子目錄有自己的.htaccess檔案,那么這可能會或可能不會起作用,具體取決于這些子子目錄.htaccess檔案中使用的指令和/或 mod_rewrite 繼承的配置方式。
mod_rewrite 指令默認不繼承。因此,如果.htaccess子目錄中的檔案啟用了重寫引擎,那么父目錄中的上述 mod_rewrite 指令將被完全覆寫(甚至不處理它們)。
另一方面,如果.htaccess子目錄中的檔案使用來自其他模塊的指令,那么這可能不是問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/476298.html
