My document root is /var/www/html/
.
I created a directory called myapi inside like /var/www/html/myapi
.
The folder structure in /var/www/html/myapi
is like
.
|-- .htaccess
|-- composer.json
|-- composer.lock
|-- src
| |-- config
| |-- index.php
`-- vendor
I want all the requests such as http:localhost/myapi/books
would be redirected to the index.php inside the src folder.
my current .htaccess
setup:
RewriteEngine On
RewriteCond %{REQUEST_URI} !(.png|.jpg|.gif|.jpeg|.zip|.css|.svg|.js)$
RewriteRule (.*) src/index.php [QSA,L]
RewriteEngine is already switched on.
But it doesn’t work and the server does not respond.
May I know how?
EDIT: FIXED. I mess up some volume mount for the .htaccess file in docker container.
2
Answers
Use this following code in .htaccess. I expect it should be work
As I mentioned in comments, this is "OK" but can be improved.
This rule unnecessarily rewrites to itself. There is no rewrite-loop here since you are rewriting to a static file-path. (But include a slash prefix on the substitution string and you get a rewrite-loop – 500 internal server error.)
There is no need to capture the entire URL-path (ie.
(.*)
), since its not being used.The condition that checks that the request does not end in a known file extension can be moved to the
RewriteRule
pattern. (TheRewriteCond
directive is not required.) Remove the dot from the regex alternation to "simplify" the regex. Consider adding.php
to the list of known file extensions, and/or use theEND
flag (Apache 2.4) instead ofL
to prevent further loops by the rewrite engine.The
QSA
flag is not required since you are not appending a query string on the substitution string. The query string is passed through by default.For example, use the following instead: