.htaccess redirection when file does not exist

I use .htaccess to redirect users to the main controller and it works fine. But when I call a js file that does not exist, for example:

<script src="/js/jquery1231231312.js"></script> 

Instead of just specifying a 404 file, this js file gets the contents of index.php. How can I continue?

This is my .htaccess

 <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] </IfModule> 
+4
source share
1 answer

Well, the way you configure it, everything goes through index.php . If a resource is requested that does not exist, it is routed through index.php . It is before index.php to implement something wrong and return a 404 error, but I assume that it is configured so that if url= does not exist to return the home page.

You can change your rules to ignore routing for specific files, for example, for your example:

 RewriteEngine on RewriteCond %{REQUEST_URI} !^/js/ RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] 

This will lead to the fact that nothing in the /js directory will ever be routed through index.php , so if someone asks for a nonexistent file in the /js directory, he will simply get the usual 404 response.


EDIT:

This is what I want to ignore the routing for certain files (js, css, images), because if the files do not exist, I get a normal error!

If you want to ignore all images, javascript and css, then the second line should be:

 RewriteCond %{REQUEST_URI} !\.(png|jpe?g|gif|css|js)$ [NC] 
+6
source

All Articles