.Htaccess多次重写不起作用

时间:2016-06-14 13:15:01

标签: .htaccess

被修改 .htacess:

RewriteEngine On
RewriteRule ^(.*)/(.*)/(.*)/(.*)/ api.php/$1/$2?param=$3&key=$4 [L]
RewriteRule ^(.*)/(.*)/(.*)/ api.php/$1/$2?param=$3 [L]
RewriteRule ^(.*)/(.*)/ api.php/$1/$2 [L]

这会出现此错误:

  

内部服务器错误

更新

此代码有效:

   RewriteEngine On
   RewriteRule ^(.*)/(.*)/(.*)/(.*)/ api.php/$1/$2?param=$3&key=$4 [L]
   RewriteRule ^(.*)/(.*)/(.*)/ api.php/$1/$2?param=$3 [L]

只有在添加此行时才会崩溃:

RewriteRule ^(.*)/(.*)/ api.php/$1/$2 [L]

2 个答案:

答案 0 :(得分:1)

它导致500内部错误,因为您的规则无限循环。这是因为您的模式^(.*)/(.*)/也匹配重写的URI api.php/<whatever>/

您需要跳过顶部现有文件和目录的所有规则:

RewriteEngine On

# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^(.*)/(.*)/(.*)/(.*)/ api.php/$1/$2?param=$3&key=$4 [L,QSA]

RewriteRule ^(.*)/(.*)/(.*)/ api.php/$1/$2?param=$3 [L,QSA]

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

我还建议您在模式中使用锚$来进一步保护。

答案 1 :(得分:0)

RewriteEngine On

RewriteRule ^(.*)/(.*)/(.*)/(.*)/ api.php/$1/$2?organisation=$3&key=$4 [L]
RewriteRule ^(.*)/(.*)/(.*)/ api.php/$1/$2?organisation=$3 [L]
RewriteRule ^(.*)/(.*)/$ api.php/$1/$2 [L]

以上解决方案可能也在运作