404 trap for non-existent files
I'll start with the fact that this post is essentially of no practical use to sites already "installed" and fully ready on the server.
Doing "one site" I copied it to the local (everything except media files), and there were a lot of pictures on the site: at least two per record plus galleries. It turned out that on each page I had at least 2 broken image links, and sometimes even 20, I didn't need the images to work on the site.
I noticed that the more images on the page, the longer the page loads, up to 10 seconds. This became annoying, and I had to figure out the reason for such outrage. The reason turned out to be that each broken link to the image "hidden" generated a 404 page. As an example, you can try entering a link like: http://yourdomain/image.jpg and you will get an error page, a page with all the contents of the site, with sidebar(s) and the like...
Close unnecessary 404 pages
It turns out that to generate a page with one broken link to a file, WordPress generates two pages: the page itself and the 404 error page. If there are 2 broken links on the page, then 3 pages will be created, and so on.
Of course, on a working site there are usually no broken links, so there is no such problem, but on the local server, it may be useful to not load the 404 page if it is a file. However, this can also be useful on a working site see below.
Option on .htaccess (recommended)
This problem can be solved by adding the following code to the functions.php file and flashing rewrite rules:
# Add conditions to .htaccess that will give a 404 server response for non-existent files. # Note: to make the code work, you need to flush the rewrite rules. if( is_admin() ){ add_filter( 'mod_rewrite_rules', 'block_nonexistent_files' ); function block_nonexistent_files( $rules ) { $add_rules = ' # 404 for non-existent files. <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !^/robots\.txt$ RewriteCond %{REQUEST_URI} \.(php|s?htm|shtml|css|js|yml|swp|txt|jpe?g|png|gif|ico|pdf)(.*)?$ RewriteRule . - [R=404,L] </IfModule> '; $add_rules = trim( $add_rules ); $add_rules = preg_replace( '/^\t+/m', '', $add_rules ); return "\n$add_rules\n\n" . $rules; } }
To make sure everything works, type http://my-domain/image.jpg in the browser and see the 404 response:
What does the code do?
As soon as a file is requested (a page containing links ending in .jpg, .gif, .png, .zip...), it is checked for physical existence, and if it does not exist, the script execution is interrupted with a 404 status and does not reach PHP and therefore WordPress. Such a trap is generated in a fraction of a second.
You can set a 404 page template in this case through the directive ErrorDocument 404 /404.html
and creating a file 404.html in the site root.
Option in php (not recommended)
The code must be inserted at the very beginning of the index.php file in the root directory of the site, where wp-config.php is located.
// Checking for a 404 error type, if it's a file, do not generate a page, // but just write about the error $URIreq = $_SERVER['REQUEST_URI']; if ( preg_match('/\.(jpg|jpeg|gif|png|zip)(\?.+)?$/', $URIreq ) ){ $PathToFileFromRoot = $_SERVER['DOCUMENT_ROOT'].$URIreq; $PathToFileFromRoot = str_replace( '//', '/', $PathToFileFromRoot ); if ( !file_exists($PathToFileFromRoot) ){ echo "<div style='margin:100px 10% 0 10%; padding:20px; text-align:center; border:1px solid #42A6FF; background:#DEF0FF; white-space:nowrap;'> <b>File not found:</b> $URIreq<br> <b>From Page:</b> <a href='{$_SERVER["HTTP_REFERER"]}'>{$_SERVER["HTTP_REFERER"]}</a><br> <div style='font-size:25px; padding-top:30px;'>Go to WebSite: <a href='http://{$_SERVER['HTTP_HOST']}'>http://{$_SERVER['HTTP_HOST']}</a></div> </div>"; exit(); } }
Attention, for already working sites!
The same effect is subject to:
- Broken links to non-existent files: images called from css styles and links to css, js, and other files.
- Non-existent files, which are often accessed by various bot scrapers, for example,
example.com/absd.php
. - Your deleted pictures, which were copied and used on other sites.
Therefore, if you feel that your page is loading suspiciously slowly, check all the links from the page. I found several broken links more than once. For example, take this site: wordpressinside.ru, which you may be familiar with, here, at first glance, I found at least 2 broken links, here they are (before writing this article ):
http://wordpressinside.ru/wp-content/plugins/simple-counters/js/sc.js.php
http://wordpressinside.ru/wp-content/plugins/simple-counters/js/jquery.qtip.js
These links are embedded in the template, and when any page is generated, 2 404 pages are generated in parallel. I think it is not difficult to imagine how much longer the page is generated, about unnecessary and absolutely unnecessary server loads I will not say...
To solve the problem, you can simply delete broken links - they are simply not needed...