【问题标题】:mod_rewrite "too many redirects" problemmod_rewrite“重定向太多”问题
【发布时间】:2009-12-02 00:10:30
【问题描述】:

尝试,

  <IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{REQUEST_METHOD} ^TRACE
  RewriteRule .* - [F]
  RewriteCond %{HTTP_HOST} ^(.*)dev\.example\.edu$ [NC]
  RewriteRule ^/test(.*)$ http://dev.example.edu/test/index.php/test$1 [NC]
  </IfModule>

在一个 apache 2.2 服务器上,让这个重写工作隐藏路径的“index.php/test”部分。

我尝试过的所有操作要么在地址栏中循环 url 部分(index.php/test),要么给出“重定向过多”错误。

我怀疑等式的“测试”部分在两边都被抛弃了,但我不确定如何让它发挥作用。

我只想: dev.example.edu/test/index.php/test* 重写为: dev.example.edu/test/*

谢谢

【问题讨论】:

    标签: mod-rewrite rewrite


    【解决方案1】:

    您需要排除目标路径以避免无限递归:

    RewriteCond %{HTTP_HOST} ^(.*)dev\.example\.com$ [NC]
    RewriteCond $1 !^/index\.php/test/
    RewriteRule ^/test/(.*)$ http://dev.example.com/test/index.php/test/$1 [NC]
    

    这里第一个分组($1)的匹配被检查为不匹配^/index\.php/test/

    但是如果你想将/test/index.php/test/…重写为/test/…,你宁愿需要这条规则:

    RewriteCond %{HTTP_HOST} ^(.*)dev\.example\.com$ [NC]
    RewriteRule ^/index\.php/test/(.*)$ http://dev.example.com/test/$1 [NC]
    

    【讨论】:

    • 我对此有点困惑。在您的第二个示例中,模式不是与匹配切换吗?我认为它是:RewriteRule Substitution(switchThis) Pattern(withthis),当我期望相反时
    • @chris:你说得对。语法当然是RewriteRule pattern substutution但是你会说“我希望将模式重写为替换”,而反之亦然。因为这就是 mod_rewrite 的工作原理。
    【解决方案2】:

    webmasterworld 的每个 Jim(谢谢!)

    “[P] 标志在指定的 URL 向服务器调用反向代理请求;也就是说,它打开一个新的传出 HTTP 连接并向该服务器发送请求。所以至少,您的仅使用原始工作规则,配置速度是应有的两倍,因为您的服务器正在通过 HTTP 向自己发送新请求,而不是仅从非默认映射文件路径提供内容。

    在我看来,所需要的只是内部重写,以便请求 URL 上的资源 http://dev.example.edu/distribution/ 与服务器文件路径 /distribution/index.php/distribution/ 处的脚本生成的内容一起提供

    RewriteEngine on 
    # 
    # Return 403-Forbidden response to TRACE requests 
    RewriteCond %{REQUEST_METHOD} ^TRACE 
    RewriteRule .* - [F] 
    # 
    # Internally rewrite requests for URL-path /recreation/<anything> 
    # to filepath /eel/index.php/recreation/<anything> 
    RewriteCond %{HTTP_HOST} ^dev\.example\.edu [NC] 
    RewriteRule ^/recreation/(.*)$ /ee1/index.php/recreation/$1 [L] 
    # 
    # Internally rewrite requests for URL-path /distribution/<anything> 
    # to filepath /distribution/index.php/distribution/<anything> 
    RewriteCond %{HTTP_HOST} ^dev\.example\.edu [NC] 
    RewriteRule ^/distribution/(.*)$ /distribution/index.php/distribution/$1 [L]
    

    所以我认为我只是让它变得比它必须的更复杂。我已经删除了 P 标志并从重写器中删除了完整的服务器地址。

    【讨论】:

      猜你喜欢
      • 2015-01-04
      • 2014-11-16
      • 1970-01-01
      • 2016-11-30
      • 2021-03-31
      • 1970-01-01
      • 2011-05-03
      • 1970-01-01
      • 2020-10-21
      相关资源
      最近更新 更多