某些REQUEST_URI上的mod_rewrite

I have a file structure like this:

ROOT
|----app/
|----app/init.php
|----public/
|----public/index.php
|----public/img/
|----public/js/
|----.htaccess

The .htaccess's rules will redirect all REQUEST_URI to app/init.php

RewriteCond %{REQUEST_URI} !\.(css|js|ttf|woff|woff2|eot|otf|svg|png|jpg)$
RewriteCond public/%{REQUEST_FILENAME} !-f
RewriteCond public/%{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ app/init.php [NC,L,QSA]

Then app/init.php will use require_once to get the appropriate .html/.php in the public folder.

This works fine; all excluded file in .htaccess will be called as such public/... pattern of path in public/index.php.

For examples:

  1. CSS <link rel="stylesheet" href="public/css/layout.css">
  2. Image <img src="public/img/abc.jpg" alt="">

However, I don't want to add public/ prefix on all the file I excluded in .htaccess

I tried adding

RewriteCond %{REQUEST_URI} \.(jpg)$
RewriteRule ^([^?]*)$ public/$1 [NC]

However, it does not work. More accurately, when I called like this img/abc.jpg in public/index.php file, it feeds back is 500 (Internal Server Error). I am guessing it is something wrong with the redirection, but I am not sure.

Question: How can I call css or img or etc.. those file without a prefix of public/ in the path?

Have it this way in your root .htaccess:

RewriteEngine On

# handle css, js and img files
RewriteRule (?:^|/)((?:img|css|js)/.+) public/$1 [L,NC]

# handle rest of the files
RewriteCond public/%{REQUEST_FILENAME} !-f
RewriteCond public/%{REQUEST_FILENAME} !-d
RewriteRule !^public/ app/init.php [L,NC]