htaccess返回内部服务器错误

时间:2013-11-13 14:47:54

标签: php regex apache .htaccess mod-rewrite

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

RewriteRule ^api/list/episode/([^/]*)$ /api/list/episode.php?season=$1 [QSA,NC,L]
RewriteRule ^api/script/([^/]*)/([^/]*)$ /api/script/?season=$1&episode=$2 [QSA,NC,L]

api/list/episode的重写规则提出Internal Server Error我不知道为什么,但其他规则有效,但我试图改变

:此 http://domain/api/list/episode?season=1

到此 http://domain/api/list/episode/1

我是否应该将.htaccess放在我的根目录中以进行所有目录更改?

2 个答案:

答案 0 :(得分:1)

那是因为循环。稍微改变一下这条规则:

RewriteEngine on 

## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]

RewriteRule ^api/list/episode/([^/]+)/?$ /api/list/episode.php?season=$1 [QSA,NC,L]
RewriteRule ^api/script/([^/]*)/([^/]*)$ /api/script/?season=$1&episode=$2 [QSA,NC,L]

RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php [L]

答案 1 :(得分:1)

尝试更改规则的顺序。两个/api/规则应该在路由到php扩展规则之前。这是因为%{REQUEST_FILENAME}实际上检查了内部路径元素,因此它可能会将/api/list/episode/1误认为/api/list/episode.php/1为“存在”,然后您将.php 添加到结尾 ,它使它:/api/list/episode/1.php。它通过重写引擎,同样的事情再次发生,然后循环继续。

RewriteEngine on 

RewriteRule ^api/list/episode/([^/]*)$ /api/list/episode.php?season=$1 [QSA,NC,L]
RewriteRule ^api/script/([^/]*)/([^/]*)$ /api/script/?season=$1&episode=$2 [QSA,NC,L]

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

你也可以尝试改变你-f check to:

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f