user2394156 user2394156 - 2 months ago 17
Apache Configuration Question

Rewriting only when a file doesn't exist

I've got a .htaccess file as it follows:

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)$ index.php [NC,L]


I've got this file in my web directory. Is there any way to force this rule to stop rewriting if there's a file?

For example: I've got a css/style.css file but the server returns 404 because the mod_rewrite rule works. If I turn the mod_rewrite off, the file is found.

Answer

Yes, there is:

<IfModule mod_rewrite.c>
  RewriteEngine On

  RewriteCond %{REQUEST_FILENAME} -f [OR]
  RewriteCond %{REQUEST_FILENAME} -d
  RewriteRule ^(.+) - [PT,L]

  RewriteRule ^assets/(.*)$ public/assets/$1 [L]
</IfModule>

First block of conditions and the first rule is applied to existing files and folders, and the second rule if an actual rewrite that should happen if there is no file or folder.

Comments