How do I skip wordpress’s 404 handling and redirect all 404 errors for static files to 404.html?
I read and it seems its not possible when using permalinks?
The objective is to reduce the server load for 404 errors by not loading php.
How do I skip wordpress’s 404 handling and redirect all 404 errors for static files to 404.html?
I read and it seems its not possible when using permalinks?
The objective is to reduce the server load for 404 errors by not loading php.
You must be logged in to post a comment.
.htaccess skip WordPress 404 error handling for static files.
Note: These rules were generated by the W3 Total Cache plugin*
Nginx skip WordPress 404 handling for static files.
To extend on what Chris_O said….I would install W3 Total Cache and use the settings from that plugin to not cache static files. The plugin in itself is very useful and a must to speed up your site, especially with the latest update.
Also I recommend you have a look at Creating an Error 404 Page from WordPress to see how to handle 404 for static files, 403 (forbidden), etc. Its a good read.
Maybe a simple solution. Use the conditional tag
is_404()
and create an redirect to your static file; include the code in the fileheader.php
orindex.php
of the theme.Here a example.
Links
I’m not sure that this is possible. If you look at the htaccess code that WordPress creates when you enable permalinks, it basically says: “If the file/directory cannot be found, send it to index.php.” This includes all actual 404 requests. Outside of creating a list of every dynamically-generated public resource that WordPress knows of and directly inserting this into .htaccess, you’ll need to load php to handle the 404’s.
I liked the idea of Chris_O, but I made my own version, which is more safe.
So what I did, I just added folders to exception, so if your requests starts from those lines – it’s definitely not a valid permalink. The majority of requests are coming from bots that are trying to check the contents of those folders for exploits. They will be effectively filtered, and if needed you can display some small static 404 page.
Other requests will still be handled by wordpress, and if someone enters a wrong address it will display a user friendly not found message within your template. The solution of Chris_O will only work for requests that look like file extensions, otherwise they will also be handled by wordpress.
To make it even more reliable, you can retrieve your raw access file and search for 404 errors. If you notice many requests that start with particular lines, you can also include them to this filter:
I have multiples CMS installed on my site so I use something like this to use the same 404 error page for all CMS’s. I use this conf for Nginx+FastCgi and it’s working fine:
I use this config together with this in php.ini :
wordpress is installed like this http://example.com/wordpress/ . the 404.html is located in the root of the http://example.com/ .
P.S. Do not forget that PHP and Nginx services need to be restarted after making changes to the php.ini or nginx.conf files for the changes to take effect.
There are 3 ways of doing this
Complete tutorial – (link dead and redirected to spam)