0

我正在尝试实现以下 mod_rewrite 规则:

host.com/developer/ => host.com/developer/index.py
host.com/developer/branchX => host.com/developer/index.py?branch=branchX
host.com/developer/branchX/commitY => host.com/developer/index.py?branch=branchX&commit=commitY

目前,适当的配置部分如下所示:

  Options FollowSymLinks
  AllowOverride None
  RewriteEngine on
  RewriteRule ^([^/]+)$                   /$1/index.py                          [L]
  RewriteRule ^([^/]+)/([^/]+)            /$1/index.py?branch=$2                [L]
  RewriteRule ^([^/]+)/([^/]+)/([^/]+)$   /$1/index.py?branch=$2&commit=$3      [L]

但是,在最初重写 URL 之后,会发生内部重定向,并且 URL 再次被重写,从而破坏了它。该过程重复多次,最终导致 500 错误。日志(时间戳和 perdir 部分已删除):

[..initial] (3) strip per-dir prefix: /home/www/host.com/master/a -> master/a
[..initial] (3) applying pattern '^([^/]+)$' to uri 'master/a'
[..initial] (3) strip per-dir prefix: /home/www/host.com/master/a -> master/a
[..initial] (3)  applying pattern '^([^/]+)/([^/]+)' to uri 'master/a'
[..initial] (2) rewrite 'master/a' -> '/master/index.py?branch=a'
[..initial] (3) split uri=/master/index.py?branch=a -> uri=/master/index.py, args=branch=a
[..initial] (1) internal redirect with /master/index.py [INTERNAL REDIRECT]
[..initial/redir#1] (3) strip per-dir prefix: /home/www/host.com/master/index.py -> master/index.py
[..initial/redir#1] (3) applying pattern '^([^/]+)$' to uri 'master/index.py'
[..initial/redir#1] (3) strip per-dir prefix: /home/www/host.com/master/index.py -> master/index.py
[..initial/redir#1] (3) applying pattern '^([^/]+)/([^/]+)' to uri 'master/index.py'
[..initial/redir#1] (2) rewrite 'master/index.py' -> '/master/index.py?branch=index.py'
[..initial/redir#1] (3) split uri=/master/index.py?branch=index.py -> uri=/master/index.py, args=branch=index.py

如何修复我的规则以防止无休止的内部重定向?谢谢。

4

1 回答 1

0

问题是您要重写的 url 在下一次传递中匹配。L 指定最后一条规则,但仅针对此规则的执行,在某些情况下(在本例中为内部重定向)再次处理 URL。解决方案是添加一个 RewriteCond 以防止循环,如下所示:

RewriteEngine on
RewriteCond %{REQUEST_URI}              !index\.py
RewriteRule ^([^/]+)$                   /$1/index.py                          [L]
RewriteCond %{REQUEST_URI}              !index\.py
RewriteRule ^([^/]+)/([^/]+)            /$1/index.py?branch=$2                [L]
RewriteCond %{REQUEST_URI}              !index\.py
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$   /$1/index.py?branch=$2&commit=$3      [L]
于 2008-10-12T10:21:21.990 回答