Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess redirecting when file doesn't exists

I'm using .htaccess to redirect users to my main controller and it is working fine. But when i call a js file that doesn't exist like:

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

Instead of just says 404 - file doesn't exists, this js file is getting the content of index.php. How should i proceed?

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>
like image 851
Guilherme Miranda Avatar asked Aug 23 '12 21:08

Guilherme Miranda


1 Answers

Well, the way that you have it setup, everything gets routed through index.php. If a request is made for a resource that doesn't exist, it gets routed through index.php. It's up to index.php to realize something's not there, and return a 404 error, but I'm guessing it's setup so that if the url= doesn't exist, to returns the home page.

You can change your rules to ignore the routing for certain files, like 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 would make it so nothing in the /js directory will ever get routed through index.php, so if someone requests a non-existing file in the /js directory, they'll just get the regular 404 response.


EDIT:

That's what I want, to ignore the routing for certain files (js, css, images), because then if the files doesn't exists i would get the normal error!

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

RewriteCond %{REQUEST_URI} !\.(png|jpe?g|gif|css|js)$ [NC]
like image 53
Jon Lin Avatar answered Nov 11 '22 16:11

Jon Lin