It is necessary that for any request first check for the existence of the requested file in the public folder. If such a file exists, then redirect to it, otherwise redirect to core.php , which itself decides what to do next.

For example:

  • site.com/favicon.ico issues a corresponding file physically located in {root directory of the site} /public/favicon.ico (if it is there, of course, there is)
  • site.com/users goes to core.php (if there is no users file in public)

In .htaccess made such a construction (part of it found on the Internet):

RewriteCond %{DOCUMENT_ROOT}/public/%{REQUEST_URI} -f RewriteRule ^(.*)$ public/$0 [L] RewriteRule ^(.*)$ core.php [L] 

As I understand it, it does (or should do) the following:

  1. The first RewriteRule will output the requested file with the prefix public, if the corresponding file exists (as verified through RewriteCond)
  2. The last rewriteRule is the default rule that works (or should work) when all previous rules did not work.

Honestly, I do not fully understand what $ 0 is. Googled about it like this: "Feedback $ N to templates in RewriteRule", but this explains absolutely nothing, if not more confusing.

The problem is that for some reason, all requests go to core.php . When I remove the last RewriteRule, it works as it should, but then there is direct access to all other files and it becomes impossible to dynamically generate virtual paths through the script. Already tried many different options, I also searched in the htaccess documentation, but in the end, it still turns out to be nonsense.

    1 answer 1

    Problem solved. It was necessary, when converting to core.php to give direct access to the public folder. That is, at first the request site.com/favicon.ico converted to site.com/public/favicon.ico , but the RewriteRule originally specified themselves themselves first look for the file in the public folder, therefore in this case the site.com/public/public/favicon.ico file will be searched site.com/public/public/favicon.ico , which, in fact, does not exist. Therefore, the request always went to core.php.

     #Если по заданному запросу существует файл в папке public RewriteCond %{DOCUMENT_ROOT}/public/%{REQUEST_URI} -f #Переходим на него #$0 - это первое выражение в скобках, то есть (.*) RewriteRule ^(.*)$ public/$0 [L] #По умолчанию всегда переходим на core.php #При этом нужно разрешить входить в папку public напрямую, #чтобы можно было реализовать правила выше RewriteRule !(^public/) core.php [L]