skip to Main Content

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


  1. Use this following code in .htaccess. I expect it should be work

    RewriteEngine On
    RewriteCond %{REQUEST_URI}  !(.png|.jpg|.gif|.jpeg|.zip|.css|.svg|.js)$
    RewriteRule (.*) src/index.php?page=$1 [QSA,L]
    
    Login or Signup to reply.
  2. RewriteCond %{REQUEST_URI}  !(.png|.jpg|.gif|.jpeg|.zip|.css|.svg|.js)$
    RewriteRule (.*) src/index.php [QSA,L]
    

    …may I know how my .htaccess could be improved?

    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. (The RewriteCond 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 the END flag (Apache 2.4) instead of L 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:

    RewriteRule !.(png|jpe?g|gif|zip|css|svg|js)$ src/index.php [END]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search