htaccess for restful apis,子目录无效

时间:2015-02-24 20:27:21

标签: .htaccess

我的apis的目录结构为:

api/
   cameras/
      index.php
      ...
      .htaccess
   bar/
      set/
         index.php
      scan/
         index.php
      retrieve/
         index.php
      list/
         index.php

在我添加bar文件夹之前,我使用了.htaccess文件夹

RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php [L]

然后我添加了它以使其工作。

RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule bar/set/(.*) bar/set/index.php [L]
RewriteRule bar/scan/(.*) bar/scan/index.php [L]
RewriteRule bar/list/(.*) bar/list/index.php [L]
RewriteRule bar/retrieve/(.*) bar/retrieve/index.php [L]
RewriteRule (.*) index.php [L]

除了列表set中的第一个外,它们都有效。我做错了什么?

2 个答案:

答案 0 :(得分:1)

我认为您的条件顺序可能会导致问题。如果没有看到您正在使用的URL,我将不得不假设。但是RewriteCond仅适用于直接跟随它的规则。您将新规则放在条件和原始规则之间。更改订单并以这种方式尝试,看看它是如何工作的。

RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
RewriteRule ^bar/set/(.*)$ bar/set/index.php [L]
RewriteRule ^bar/scan/(.*)$ bar/scan/index.php [L]
RewriteRule ^bar/list/(.*)$ bar/list/index.php [L]
RewriteRule ^bar/retrieve/(.*)$ bar/retrieve/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php [L]

答案 1 :(得分:1)

RewriteCond仅适用于下一个RewriteRule。请尝试使用此代码:

DirectoryIndex index.php
RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%1]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^(bar)/(scan|set|scan|retrieve)/(.*)$ $1/$2/index.php [L,NC]

RewriteRule . index.php [L]