簡體   English   中英

mod_rewrite 使用 URL 中的絕對路徑重定向

[英]mod_rewrite redirects with absolute path in URL

我正在嘗試使用 Apache mod_rewrite 我做的第一件事是將我的 url 重寫為一個工作正常的index.php文件。 但我認為我也應該刪除尾部斜杠(es),因為我更希望由 Apache 而不是我的 PHP 路由器來處理它。

這是我的.htaccess文件的全部內容:

RewriteEngine on

# one of the attempts to remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.*)/+$
RewriteRule ^(.*)/+$ $1 [R=301,L]

# This is the rewriting to my index.php (working)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [L]


問題:
我閱讀了幾個關於刪除尾部斜杠的問題,但找不到適合我的答案:
對於我嘗試的每個答案,我都能夠訪問我的 PHP 路由器index.php (位於Phunder\\public\\ )而沒有尾部斜杠:

 // Requested URL | No redirection http://localhost/projects/Phunder/public/home | http://localhost/projects/Phunder/public/home

但是,當請求帶有斜杠的同一頁面時,我會使用包含的絕對路徑進行重定向:

 // Requested URL | Wrong redirection http://localhost/projects/Phunder/public/home/ | http://localhost/C:/xampp/htdocs/projects/Phunder/public/home


其他信息:

  • 我總是在測試時清除緩存
  • 將我的最后一個RewriteRule更改為RewriteRule ^(.*)/?$ index.php?/$1 [L]導致404 錯誤,URL 帶有尾部斜杠。
  • 實際錯誤的重定向會導致403 錯誤

我是 mod_rewrite 的初學者,我並不總是理解我的嘗試(遺憾的是)。 有什么我錯過或誤用的嗎? 我該怎么做才能獲得預期的行為?

重定向規則需要絕對 URL 或RewriteBase 您也可以從%{REQUEST_URI}提取完整的 URI,如下所示:

RewriteEngine on

# one of the attempts to remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/+$
RewriteRule ^ %1 [R=301,NE,L]

# This is the rewriting to my index.php (working)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM