RewriteRule 500错误问题

时间:2011-04-20 22:54:23

标签: php apache .htaccess mod-rewrite apache2

我设置了以下重写规则:

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标志,一旦它击中某个东西就会停止执行。或者我的语法错了?

干杯

3 个答案:

答案 0 :(得分:2)

  1. 每当您获得500时,请检查/var/log/httpd/error_log(或系统上的等效路径。)
  2. 我很确定你的字符组中的连字符是一个正则表达式语法错误。 (另外,[NC]标志使[A-Za-z]多余
  3. 尝试:

    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]
    

答案 1 :(得分:-1)

我认为您需要QSA flag,请尝试:

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

答案 2 :(得分:-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是很重要的,以避免无限重定向。