8

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

Here's what I've got:

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]

e.g. http://example.com/css/style.css should have apache serve /public/css/style.css because it's a file that exists, but http://example.com/css/style.bad should be sent to /index.php.

mpen
  • 272,448
  • 266
  • 850
  • 1,236

2 Answers2

6

This code should be working as expected 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]
Justin Iurman
  • 18,954
  • 3
  • 35
  • 54
  • 1
    That works! Thank you. I just noticed though that `style.css` is available at *both* `/css/style.css` *and* `/public/css/style.css` though. Technically the latter should route to `index.php` too since `/public/public/css.style.css` is not a file. This also means `/public/xxx` will throw an apache 404 instead of the custom one I've configured in my `index.php` file. No way to fix that, is there? – mpen Sep 21 '14 at 17:28
5

Apparently [L] does not work 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]
Community
  • 1
  • 1
mpen
  • 272,448
  • 266
  • 850
  • 1,236