【问题标题】:How to redirect index.php/controller/action to controller/action without redirect loop?如何在没有重定向循环的情况下将 index.php/controller/action 重定向到 controller/action?
【发布时间】:2012-10-24 20:10:05
【问题描述】:

已解决:)

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 重定向)。但现在我得到了无限循环的重定向。

有没有机会将内部重定向分离到引导 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]

【问题讨论】:

  • 你不应该真的需要用第一个条件/规则重写......因为两个网址应该做完全相同的事情。如果他们没有你有一个糟糕的实施;-)
  • 第一条规则使外部重定向(301)第二条内部重定向。所以它不一样;)。当用户打开带有 index.php 的 url 时,他应该重定向到没有 index.php 的版本。但是没有index.php的版本必须内部重定向到引导文件index.php,这就是冲突。
  • Opps...你是对的...完全错过了。但是,如果您使用相同的 shceme,只需将 url 按原样重定向到新主机应该做同样的事情。我的意思是,就您的路由器而言,/index.php/controller/action/controller/action 相同。通常,您的最后一条规则如下所示:RewriteRule ^(.*)$ /index.php [L] 不是缺少捕获组。

标签: php .htaccess mod-rewrite


【解决方案1】:

在 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.');
}

【讨论】:

  • 但是我没有寻找停止循环的解决方案 - 我想通过区分重写条件中的内部和外部路由来防止循环:)
猜你喜欢
  • 2013-05-01
  • 2016-12-22
  • 2013-01-08
  • 2022-10-24
  • 2022-08-18
  • 1970-01-01
  • 2017-06-30
  • 1970-01-01
  • 2013-06-18
相关资源
最近更新 更多