Reputation: 693
I am developing a website in laravel 4.2 and I have used a custom 404 page to display "page not found". Everything works fine but if there are any assets that are not found, like images or js files, the Chrome console shows me the entire 404 page. For every missing asset, the entire 404 page is loaded which is very heavy and not required.
What can i do to return a simple message for assets not found instead of an entire 404 page?
Upvotes: 1
Views: 1368
Reputation: 693
I found the solution. Following nginx settings work and display nginx 404 for files not found and custom page for url not found.
rewrite_log on;
try_files $uri $uri/ @rewrite;
location @rewrite {
rewrite ^/(.*)$ /index.php?_url=/$1;
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
Upvotes: 0
Reputation: 152860
The .htaccess
in your public folder is configured to let Laravel handle everything that isn't a file or directory. A simple way to change that for a subdirectory of public
, say public/css
, is to just put a .htaccess
file in this directory and disable Larvel's routing for that folder:
public/css/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine Off
</IfModule>
Then you will get the default Not found page of your webserver when trying to access a non-existing file.
Sidenote: Of course you could also accomplish this using Laravel and some program logic, but this seems the cleanest approach as it doesn't even start up Laravel and thus saves response time and server resources.
Also you should probably do your best to only request files that actually exist to avoid 404s.
Upvotes: 3