将URL重写为index.php,但要避免URL中的index.php

时间:2010-05-30 12:55:23

标签: apache .htaccess mod-rewrite

我正在尝试将所有请求内部重定向到index.php,并使用.htaccess文件从外部重定向包含index.php的所有请求。

所以像http://host/test这样的网址应该由index.php处理,像http://host/index.php/test这样的网址应该重定向到http://host/test,然后由index.php处理(不将浏览器重定向到索引。 PHP)

我尝试了以下操作,但总是收到“太多重定向...”的消息:

RewriteRule ^index\.php/?(.*)$ /$1 [R,L]
RewriteRule .* index.php/$0 [L]

2 个答案:

答案 0 :(得分:2)

您需要查看请求行中的网址,了解是否已请求/index.php/…

RewriteCond %{THE_REQUEST} ^GET\ /index\.php/?([^ ]*)
RewriteRule ^index\.php/?(.*) /$1 [R,L]
RewriteCond $0 !^index\.php($|/)
RewriteRule .* index.php/$0 [L]

答案 1 :(得分:0)

除其他外,如果您想在不重定向浏览器的情况下执行此操作,则不希望使用[R]选项,这意味着R会直接浏览器。

试试这个:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^(index.php/)?.* index.php [L]
</IfModule>