[英]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]
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 ^(.*)/?$ index.php?/$1 [L]
導致404 錯誤,URL 帶有尾部斜杠。我是 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.