2011-04-20 57 views
1

我有以下重写规则设置:重写规则500错误问题

RewriteEngine On 

RewriteRule ^api/([A-Za-z0-9-]+)$ index.php/api/$1 [NC,L] 
RewriteRule ^([A-Za-z0-9-]+)$ index.php/other/$1 [NC,L] 

不幸的是这导致我的服务器抛出一个500错误。单独采取,但他们都很好,但。

我的本意是,如果该请求是http://somesite.com/api/whatever/,第一条规则将被触发,重定向到index.php/api/whatever/

如果不是“API”的任何被作为第二段虽然发送,它会重定向到index.php/other/whatever

我的理解有瑕疵吗?我认为它会在列表中下来,并且用L标志,一旦碰到什么就停止执行。或者我的语法错了?

干杯

回答

-1

我想你需要QSA flag,尝试这样的:(或您的系统上的等价路径)

RewriteRule ^api/(.*)$ index.php/api/$1 [QSA,NC,L] 
RewriteRule ^(.*)$ index.php/other/$1 [QSA,NC,L] 
+0

由于没有人评论downvote:[QSA]用于修改查询字符串并希望原始附加到您在重写中定义的新字符。这里没有查询字符串(所以它保持不变) – 2011-04-21 09:55:25

+0

感谢您的解释。但是我在我的本地配置上尝试了这些重写规则,并且只有使用QSA标志才能正常工作。 – Bil 2011-04-21 10:05:03

2
  1. 每当你得到一个500,检查/var/log/httpd/error_log
  2. 我很确定你的字符组中的连字符是一个正则表达式的语法错误。 (另外,[NC]标志使[A-Za-z]冗余

尝试:

RewriteRule ^api/([-A-Z0-9]+)$ index.php/api/$1 [NC,L] 
RewriteRule ^([-A-Z0-9]+)$ index.php/other/$1 [NC,L] 

或许

RewriteRule ^api/([^/]+)$ index.php/api/$1 [NC,L] 
RewriteRule ^([^/]+)$ index.php/other/$1 [NC,L] 
+0

+1要匹配字符类中的连字符,它应该是打开后的第一个字符'[' - http://www.regular-expressions.info/charclass.html – Phil 2011-04-21 00:45:52

-1

尝试这些规则.htaccess文件:

RewriteEngine on 
Options +FollowSymlinks -MultiViews 

RewriteRule ^api/(.*)/?$ /index.php/api/$1 [NC,L] 

RewriteCond %{REQUEST_URI} !^/index\.php/ [NC] 
RewriteRule ^(.*)/?$ /index.php/other/$1 [NC,L] 

它添加这个很重要在第二个RewriteRule之前重写RewriteCond以避免无限重定向。