如何在没有重定向循环的情况下将index.php / controller / action重定向到controller / action?

时间:2012-10-24 20:10:05

标签: php .htaccess mod-rewrite

已解决:)

RewriteCond %{THE_REQUEST} index.php
RewriteRule index.php(.*)$ http://%{HTTP_HOST}$1 [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php/$1 [L]

我配置了一个重定向,以便我可以打开http://example.com/controller/action和ModRewrite将请求内部路由到... index.php / controller / action。

现在我想重新定向用户,如果他自动打开http://example.com/index.php/controller/action到index.php / controller / action(301重定向)。但现在我得到了无限循环的重定向。

是否有机会将内部重定向分离到bootstrap index.php并阻止,一个用户可以使用index.php子路径打开url?

我尝试了这个,但那不起作用(无限循环):

 RewriteCond %{REQUEST_URI} index.php/(.*)
 RewriteRule ^index.php/(.*)/$ http://%{HTTP_HOST}/$1/ [L,R=301]

 RewriteCond %{REQUEST_FILENAME} !-d 
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteRule ^(.*)$ /index.php/$1 [L]

1 个答案:

答案 0 :(得分:0)

在PHP方面解决这个问题的常用方法是让计数器随每次重定向递增。例如:https://github.com/vjousse/symfony-1.4/blob/master/lib/controller/sfController.class.php

if ($this->getActionStack()->getSize() >= $this->maxForwards)
{
  // let's kill this party before it turns into cpu cycle hell
  throw new sfForwardException('Too many forwards have been detected for this request.');
}