一个mod_rewrite问题

时间:2008-10-12 09:44:30

标签: apache mod-rewrite

我正在尝试实现以下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

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

1 个答案:

答案 0 :(得分:0)

问题是你要重写的网址在下一次传递中是匹配的。 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]