.htaccess: serve static files, redirect everything else to index.php

I want to use the .htaccess file to check if the requested path is a file in the public/ directory. If so, submit it, and then forward the request to /index.php . I can't seem to get this to work.

Here is what I have:

 Options +FollowSymLinks RewriteEngine on Options -Indexes RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -f RewriteRule ^ %{DOCUMENT_ROOT}/public%{REQUEST_URI} [L] RewriteRule ^ index.php [QSA,L] 

eg. http://example.com/css/style.css must have apache serve /public/css/style.css because it is a file that exists, but http://example.com/css/style.bad must be sent to /index.php .

+6
source share
2 answers

Apparently [L] not working as I expected . With that in mind, and a lot of trial and error, I managed to find something that works:

 RewriteEngine On RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -f RewriteRule ^ public%{REQUEST_URI} [L] RewriteCond %{REQUEST_URI} !^/public/ RewriteRule ^ index.php [QSA,L] 
+2
source

This code should work as soon as possible on your side.

 Options +FollowSymLinks -MultiViews -Indexes RewriteEngine On RewriteBase / RewriteCond %{DOCUMENT_ROOT}/public/$1 -f RewriteRule ^(.*)$ public/$1 [L] RewriteCond %{THE_REQUEST} \s/public/ [NC,OR] RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L,QSA] 
+6
source

Source: https://habr.com/ru/post/975600/


All Articles