【问题标题】:Rewrite URL to index.php but avoid index.php in the URL将 URL 重写为 index.php 但避免在 URL 中使用 index.php
【发布时间】:2010-05-30 12:55:23
【问题描述】:
我正在尝试在内部将所有请求重定向到 index.php,并在外部使用 .htaccess 文件将所有包含 index.php 的请求重定向。
所以像http://host/test这样的URL应该被index.php处理,像http://host/index.php/test这样的URL应该被重定向到http://host/test然后被index.php处理(而不是将浏览器重定向到index.php)
我尝试了以下方法,但总是收到一条消息“重定向太多...”:
RewriteRule ^index\.php/?(.*)$ /$1 [R,L]
RewriteRule .* index.php/$0 [L]
【问题讨论】:
标签:
apache
.htaccess
mod-rewrite
【解决方案1】:
您需要查看请求行中的 URL 以查看是否已请求 /index.php/…:
RewriteCond %{THE_REQUEST} ^GET\ /index\.php/?([^ ]*)
RewriteRule ^index\.php/?(.*) /$1 [R,L]
RewriteCond $0 !^index\.php($|/)
RewriteRule .* index.php/$0 [L]
【解决方案2】:
除其他外,如果您想在不重定向浏览器的情况下执行此操作,那么您不想使用[R] 选项,这意味着Redirect 浏览器。
试试这个:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(index.php/)?.* index.php [L]
</IfModule>