mod_rewrite 在 URL 中使用绝对路径重定向
mod_rewrite redirects with absolute path in URL
我正在尝试使用 Apache mod_rewrite。我做的第一件事是将我的 url 重写为一个工作正常的 index.php
文件。但我认为我也应该删除尾部斜杠,因为我更希望它由 Apache 而不是我的 PHP 路由器处理。
这是我的 .htaccess
文件的全部内容:
RewriteEngine on
# one of the attempts to remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.*)/+$
RewriteRule ^(.*)/+$ [R=301,L]
# This is the rewriting to my index.php (working)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/ [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?/ [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?/ [L,QSA]
我正在尝试使用 Apache mod_rewrite。我做的第一件事是将我的 url 重写为一个工作正常的 index.php
文件。但我认为我也应该删除尾部斜杠,因为我更希望它由 Apache 而不是我的 PHP 路由器处理。
这是我的 .htaccess
文件的全部内容:
RewriteEngine on
# one of the attempts to remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.*)/+$
RewriteRule ^(.*)/+$ [R=301,L]
# This is the rewriting to my index.php (working)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/ [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?/ [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?/ [L,QSA]