Hello. I am writing this article to share my own ideas (although, maybe, many people do this - I am not up to date).
There are many articles to improve the format of the page “Page not found” for Wordpress, and now I invite you to get acquainted with another one.
1. Wrong page address
I think this happens when the address of your post is
site.com/post.html , and the user goes to
site.com/post or
site.com/post . In this case, your entry will not be found. But after all, it is on the site - just at a different address!
Therefore, I decided to make a 301 redirect from the page with the wrong address to the "correct" one.
(I just want to admit that I am not strong in regular expressions, so I solved the problem as I could. If experienced users, instead of criticism, leave comments in the comments for improving and optimizing my code, I will be only too happy.)
Insert the code into the very beginning of the 404.php file:
$link = $_SERVER['REQUEST_URI'];
Thus, the page will be redirected to the correct address and the redirect address will be recorded in the browser’s cache.
2. Random entries
Many advised to display other entries on page 404. I agree with that.
Add to the 404.php file the output cycle code:
<?php query_posts('orderby=rand&showposts=10'); while ( have_posts() ) : the_post(); get_template_part( 'content', '404' ); ?>
')
3. Plans for the future
Unfortunately, my approach does not handle the address of the site.com/post.ht type, in the future I plan to learn regular expressions and eliminate this drawback.
In addition, it would be appropriate to display not random entries, but only those that are similar to the one you were looking for.
There are algorithms for comparing the degree of similarity of two lines? If someone uses, or just familiar with one of them - I will be glad if you share.