{"id":34590,"date":"2013-07-11T14:47:15","date_gmt":"2013-07-11T09:17:15","guid":{"rendered":"https:\/\/www.quickonlinetips.com\/archives\/?p=34590"},"modified":"2021-10-25T19:45:37","modified_gmt":"2021-10-25T14:15:37","slug":"force-404-errors-wordpress","status":"publish","type":"post","link":"https:\/\/www.quickonlinetips.com\/archives\/2013\/07\/force-404-errors-wordpress\/","title":{"rendered":"How to Force 404 Errors in WordPress"},"content":{"rendered":"<p>Sometimes you have to force 404 errors in WordPress, either because the 404 pages are not properly configured, or sometimes WordPress itself or caching plugins may make 404 errors impossible.<\/p>\n<p>I tried to <a title=\"Delete Urls With Bad Backlinks to Remove Google Penalty\" href=\"https:\/\/www.quickonlinetips.com\/archives\/2013\/06\/delete-urls-with-bad-backlinks-to-remove-google-penalty\/\">create 404 pages<\/a> to fix Google penalties, but even after unpublishing posts, the site HTTP response headers still displayed a 200 OK working page. You can easily track your 404 pages using the <a href=\"https:\/\/www.quickonlinetips.com\/archives\/2021\/10\/ahrefs-free-account\/\">ahrefs free account<\/a> for your website.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-34603\" src=\"https:\/\/www.quickonlinetips.com\/archives\/wp-content\/uploads\/404-page.jpg\" alt=\"404 page\" width=\"419\" height=\"258\" srcset=\"https:\/\/www.quickonlinetips.com\/archives\/wp-content\/uploads\/404-page.jpg 419w, https:\/\/www.quickonlinetips.com\/archives\/wp-content\/uploads\/404-page-300x184.jpg 300w\" sizes=\"(max-width: 419px) 100vw, 419px\" \/><\/p>\n<p>I found that there are several ways to force 404 errors in WordPress&#8230;<\/p>\n<h2>Check 404 file exists<\/h2>\n<p>It is possible that WordPress is not configured properly to show a 404 page when it is required to do so. First, ensure your active WordPress theme has a <strong>404.php<\/strong> file in the active theme&#8217;s folder. If not, you can simply save a 404.php file with the following text.<\/p>\n<pre><code>&lt;?php get_header(); ?&gt;\r\n    &lt;h1&gt;Page Not Found&lt;\/h1&gt;\r\n&lt;?php get_sidebar(); ?&gt;\r\n&lt;?php get_footer(); ?&gt;<\/code><\/pre>\n<h2>Check 404 file works<\/h2>\n<p>Then ensure that your server can identify the location of your 404.php file. First, test the exact location of the file in your browser to see it loads. Then you can add the following code on top of your WordPress folder <strong>.htaccess<\/strong> file to let it find the file.<\/p>\n<p><code>ErrorDocument 404 \/index.php?error=404<\/code><\/p>\n<p>If your WordPress installation is in a subdirectory called &#8216;abcd&#8217;, you might use this code.<\/p>\n<p><code>ErrorDocument 404 \/abcd\/index.php?error=404<\/code><\/p>\n<h2>Disable 404 Caching<\/h2>\n<p>We had another issue with 404 files that at first instance it showed a 404 error, then our caching plugin QuickCache cached the file to create an HTML file for that error page. Then if you <a href=\"https:\/\/www.quickonlinetips.com\/projects\/http-headers-check\/\">check site headers<\/a> the page gave a 200 OK status meaning the page existed well \u00a0(this was similar to the<a title=\"WordPress Caching Plugins Can Delay RSS Feed Updates\" href=\"https:\/\/www.quickonlinetips.com\/archives\/2013\/03\/wordpress-caching-plugin-delay-rss-feed-updates\/\"> feed caching<\/a> issue).<\/p>\n<p>So we added this code to our<strong> 404.php<\/strong> template page in the first line even before the header coding was called for. \u00a0Add the following code<\/p>\n<pre><code>&lt;?php\r\nif (is_404()) {\r\n    define('QUICK_CACHE_ALLOWED', false);\r\n}\r\n?&gt;<\/code><span style=\"font-family: Consolas, Monaco, monospace; font-size: 12px; line-height: 18px;\">\u00a0<\/span><\/pre>\n<h2>Disable WordPress Smart Redirect<\/h2>\n<p>WordPress is a smart blogging tool and redirects 404 errors to the best possible matching URLs which it seems fit based on the visited URL keywords. Many of my 404 URLs were being redirected to similar working URLs, which might or might not be relevant but had similar keywords.<\/p>\n<p>So we need to add the following code to theme<strong> functions.php<\/strong> to prevent this WordPress redirection ability to get pure 404 errors.<\/p>\n<p><code>remove_filter('template_redirect', 'redirect_canonical');<\/code><\/p>\n<h2>Test Site Response Headers<\/h2>\n<p>So mostly this should take care of your 404 errors and you can test them with popular site HTTP headers checking tools like Web Sniffer\u00a0or\u00a0<a href=\"https:\/\/www.quickonlinetips.com\/projects\/http-headers-check\/\" target=\"_blank\" rel=\"noopener noreferrer\">HTTP Response Headers<\/a>\u00a0to find out if your 404 errors are actually working.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-34596\" src=\"https:\/\/www.quickonlinetips.com\/archives\/wp-content\/uploads\/404-not-found.png\" alt=\"404 not found error\" width=\"329\" height=\"119\" srcset=\"https:\/\/www.quickonlinetips.com\/archives\/wp-content\/uploads\/404-not-found.png 329w, https:\/\/www.quickonlinetips.com\/archives\/wp-content\/uploads\/404-not-found-300x108.png 300w\" sizes=\"(max-width: 329px) 100vw, 329px\" \/><\/p>\n<p>If you get this &#8216;404 Not Found&#8217; error\u00a0<em>after<\/em>\u00a0repeatedly refreshing the result you are done.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you have to force 404 errors in WordPress, either because the 404 pages are not properly configured, or sometimes&#8230;<\/p>\n","protected":false},"author":4,"featured_media":34603,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[6],"tags":[52,49,17],"class_list":["post-34590","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blogging","tag-popular","tag-tutorials","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/www.quickonlinetips.com\/archives\/wp-json\/wp\/v2\/posts\/34590","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.quickonlinetips.com\/archives\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.quickonlinetips.com\/archives\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.quickonlinetips.com\/archives\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.quickonlinetips.com\/archives\/wp-json\/wp\/v2\/comments?post=34590"}],"version-history":[{"count":0,"href":"https:\/\/www.quickonlinetips.com\/archives\/wp-json\/wp\/v2\/posts\/34590\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.quickonlinetips.com\/archives\/wp-json\/wp\/v2\/media\/34603"}],"wp:attachment":[{"href":"https:\/\/www.quickonlinetips.com\/archives\/wp-json\/wp\/v2\/media?parent=34590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.quickonlinetips.com\/archives\/wp-json\/wp\/v2\/categories?post=34590"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.quickonlinetips.com\/archives\/wp-json\/wp\/v2\/tags?post=34590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}