简体   繁体   English

REST API的CakePHP .htaccess异常

[英]CakePHP .htaccess exception for REST API

I am creating REST API with CakePHP. 我正在使用CakePHP创建REST API。 I have my Angular app in the app/webroot/js folder. 我在app / webroot / js文件夹中有我的Angular应用程序。 ui-router is working, but I'm trying to get htaccess to make an exception for urls that have /rest/ in them so that I can have rest calls like this: /rest/posts.json ui-router在工作,但是我试图让htaccess为其中包含/ rest /的URL设置例外,以便我可以进行如下的rest调用:/rest/posts.json

Here is my app/webroot/.htaccess: 这是我的app / webroot / .htaccess:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule (.*)$ index.php/#$1 [L]
    RewriteRule ^rest/.* rest/$1 [L]

</IfModule>

Why is RewriteRule ^rest/.* rest/$1 [L] not making an exception to the index.php catchall above it? 为什么RewriteRule ^rest/.* rest/$1 [L]不在上面的index.php catchall上作例外?

You answered that yourself, didn't you? 你回答自己,不是吗? The rule above is a catchall , it will catch everything, and therefore the following rule will never be reached (unless you are requesting an actual file/directory). 上面的规则是一个包罗万象的规则,它将捕获所有内容,因此将永远不会遵循以下规则(除非您请求的是实际文件/目录)。

If you'd wanted to point rest/* URLs to a different place than index.php#... , then you'd have to place it above. 如果您想将rest/* URL指向与index.php#...不同的位置,则必须将其放置在上方。 However you'd probably start to repeat yourself, as it should be subject to the "not file/directory" conditions too I guess, so I'd probably use a skipping rule for the FILENAME conditions, something like 但是,您可能会开始重复一遍,因为我猜它也应受“非文件/目录”条件的约束,因此我可能会对FILENAME条件使用跳过规则,例如

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [S=2]
RewriteRule ^rest/.* index.php [L]
RewriteRule (.*)$ index.php/#$1 [L]

which basically says, if the requested resource is a directory or a file, skip the next two rules, and if it's not, then first check if the current URL starts with rest/ , and forward it to the normal CakePHP process instead of using the hash append variant. 基本上是说,如果请求的资源是目录或文件,请跳过接下来的两个规则,如果不是,则首先检查当前URL是否以rest/开头,并将其转发到正常的CakePHP进程,而不是使用哈希追加变体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM