简体   繁体   中英

CakePHP .htaccess exception for REST API

I am creating REST API with CakePHP. I have my Angular app in the app/webroot/js folder. 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

Here is my 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?

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. 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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