Ad

How Can I Use Mod_rewrite To Redirect To Index.php Except For Assets?

- 1 answer

I would like to have mod_rewrite go into effect for any filenames that don't exists except if that file name ends with js, css, gif, etc., so those would return regular 404s...

I tried this out:

RewriteCond %{REQUEST_FILENAME} \.(js|ico|gif|jpg|png|css|pdf)$ [OR]
RewriteCond %{REQUEST_FILENAME} favicon.ico$ [OR]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ /index.php/$1 [L]

Which I found online somewhere, but it doesn't seem to work. My non existant JS files still get routed to my index.php file.

Ad

Answer

Try this rule:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !.*\.(js|ico|gif|jpg|png|css|pdf)$ /index.php%{REQUEST_URI} [L]
Ad
source: stackoverflow.com
Ad