问题
I have problems with a htaccess, maybe you can help me. My system consists of an angular2-app (single page client side app, index.html) and a slim-v3 php REST API (index.php). The angular2-app accesses the REST API. If possible, then I would like to use the same domain for the angular2-app and the slim REST API.
At the moment, the htaccess looks like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]
If i start the angular2-app via index.html and go to content like e.g. http://www.example.com/mycontent/id
then everything works fine.
If I call the URL http://www.example.com/mycontent/id
directly, then I get a 404 from Slim (PHP)
When i change the htaccess to:
RewriteEngine On
RewriteBase /
RewriteRule ^ index \ .html $ - [L]
RewriteCond% {REQUEST_FILENAME}! -f
RewriteCond% {REQUEST_FILENAME}! -d
RewriteRule. / Index.html [L]
the direct call of http://www.example.com/mycontent/id
works correctly, but the Slim-Framework does not work anymore.
Does one have an idea? Thank you!
回答1:
I came across the same issue, i solved it by having all my api's living in and pointing to
/api/index.php
Then my .htaccess looks like this:
RewriteEngine On
# Handle API Requests
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/api/
RewriteRule ^ /api/index.php [L]
## pure angular routes
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
So now my Angular app is at root, php API in /api and all live under the same roof as one happy family
来源:https://stackoverflow.com/questions/44618575/htaccess-slim-and-angular2