Redirect 404 To Homepage - Version 1.0.1

Version Description

  • fixed plugin meta data (description, etc)
Download this release

Release Info

Developer littlebizzy
Plugin Icon 128x128 Redirect 404 To Homepage
Version 1.0.1
Comparing to
See all releases

Version 1.0.1

Files changed (3) hide show
  1. 404-redirect.php +52 -0
  2. 404-to-homepage.php +66 -0
  3. readme.txt +89 -0
404-redirect.php ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * 404 To Homepage - Redirect class
5
+ *
6
+ * @package 404 To Homepage
7
+ * @subpackage 404 To Homepage Redirect
8
+ */
9
+ class NTFTHP_Redirect {
10
+
11
+
12
+
13
+ /**
14
+ * Do the redirect in a header-clean way
15
+ */
16
+ public static function go() {
17
+
18
+ // Remove existing headers
19
+ self::remove_headers();
20
+
21
+ // Permanent redirect
22
+ wp_redirect(home_url(), 301);
23
+
24
+ // End
25
+ die;
26
+ }
27
+
28
+
29
+
30
+ /**
31
+ * Remove any existing header
32
+ */
33
+ private static function remove_headers() {
34
+
35
+ // Check headers list
36
+ $headers = @headers_list();
37
+ if (!empty($headers) && is_array($headers)) {
38
+
39
+ // Check header_remove function (PHP 5 >= 5.3.0, PHP 7)
40
+ $by_function = function_exists('header_remove');
41
+
42
+ // Enum and clean
43
+ foreach ($headers as $header) {
44
+ list($k, $v) = array_map('trim', explode(':', $header, 2));
45
+ $by_function? @header_remove($k) : @header($k.':');
46
+ }
47
+ }
48
+ }
49
+
50
+
51
+
52
+ }
404-to-homepage.php ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ Plugin Name: 404 To Homepage
4
+ Plugin URI: https://www.littlebizzy.com/plugins/404-to-homepage
5
+ Description: Redirects all 404 (Not Found) errors to the homepage for a better user experience, less abuse from bots, and 100% elimination of Google GSC warnings.
6
+ Version: 1.0.1
7
+ Author: LittleBizzy
8
+ Author URI: https://www.littlebizzy.com
9
+
10
+ This program is free software; you can redistribute it and/or modify
11
+ it under the terms of the GNU General Public License as published by
12
+ the Free Software Foundation; either version 2 of the License, or
13
+ (at your option) any later version.
14
+
15
+ This program is distributed in the hope that it will be useful,
16
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
+ GNU General Public License for more details.
19
+
20
+ You should have received a copy of the GNU General Public License
21
+ along with this program; if not, write to the Free Software
22
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
+
24
+ Copyright 2016 by LittleBizzy
25
+
26
+ */
27
+
28
+
29
+ /* Initialization */
30
+
31
+ // Avoid script calls via plugin URL
32
+ if (!function_exists('add_action'))
33
+ die;
34
+
35
+ // This plugin constants
36
+ define('NTFTHP_FILE', __FILE__);
37
+ define('NTFTHP_PATH', dirname(NTFTHP_FILE));
38
+ define('NTFTHP_VERSION', '1.0');
39
+
40
+
41
+ /* 404 hooks */
42
+
43
+ /**
44
+ * Early method to detect 404 context
45
+ * Minimum WP version: 4.5.0
46
+ */
47
+ add_filter('pre_handle_404', 'ntfthp_pre_handle_404', 0, 2);
48
+ function ntfthp_pre_handle_404($preempt, $wp_query) {
49
+ if ($wp_query->is_404()) {
50
+ require_once(NTFTHP_PATH.'/404-redirect.php');
51
+ NTFTHP_Redirect::go();
52
+ }
53
+ return $preempt;
54
+ }
55
+
56
+ /**
57
+ * Detect 404 on 'wp' hook
58
+ * For any WP version
59
+ */
60
+ add_action('wp', 'ntfthp_wp');
61
+ function ntfthp_wp() {
62
+ if (is_404()) {
63
+ require_once(NTFTHP_PATH.'/404-redirect.php');
64
+ NTFTHP_Redirect::go();
65
+ }
66
+ }
readme.txt ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === 404 To Homepage ===
2
+ Contributors: littlebizzy
3
+ Donate link:
4
+ Tags: 404, not found, error, errors, missing, 301, redirect, redirection, page, homepage
5
+ Requires at least: 4.4
6
+ Tested up to: 4.7
7
+ Stable tag: 1.0.1
8
+ License: GPLv2 or later
9
+ License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
+
11
+ Redirects all 404 (Not Found) errors to the homepage for a better user experience, less abuse from bots, and 100% elimination of Google GSC warnings.
12
+
13
+ == Description ==
14
+
15
+ 404 To Homepage is a simple WordPress plugin for redirecting all 404 "Not Found" errors to the homepage. The reason for doing this is to avoid user confusion (or lost leads), and to avoid abuse causes by bots that ping your site with non-existent URLs which can negatively effect search engine indexing. Additionally, it can totally eliminate the warnings created in Google GSC (Webmasters) in regard to 404 errors that begin piling up over time, which quite often are not even the fault of your website.
16
+
17
+ It should be noted, however, that this method is not recommended for all websites. For example, large blogs or magazines that rely heavily on search engine traffic (such as a newspaper) should probably not use this plugin. This is because in many cases, 404 errors should be analyzed on a regular basis and then 301 redirected to the appropriate page. Rather, this plugin is best suited for small businesses or websites with a limited amount of content, and limited staff, who are not publishing massive amounts of content. In such cases, we suggest monitoring 404 errors for perhaps a few months, redirecting the ones that are legitimate, and then consider activating this plugin after that point.
18
+
19
+ Unlike other 404 redirect plugins, 404 To Homepage "removes" any pre-existing HTTP headers, and executes ONLY a 301 redirect header pointed at the site's homepage. In other words, it does not allow any "previous" headers to be sent in order to avoid confusing browsers or Google bots. (NOTE: while any previous headers sent by WordPress/PHP engine are ignored, it's possible for your server i.e. Apache or Nginx to override things with header rules of their own... please check to ensure no conflicts.)
20
+
21
+ __Compatibility:__
22
+
23
+ * __OS:__ Linux
24
+ * __SERVER:__ Apache, Nginx
25
+ * __PHP:__ 5.5+
26
+ * __DATABASE:__ MySQL
27
+
28
+ __Plugin Features:__
29
+
30
+ * __SETTINGS:__ None
31
+ * __MUST-USE:__ Supported
32
+ * __MULTI-SITE:__ Support unknown
33
+ * __TRANSIENTS:__ None
34
+ * __WP_OPTIONS:__ None
35
+ * __LOCALIZATION:__ None
36
+ * __UNINSTALL:__ Removes plugin files only (no stored data exists)
37
+
38
+ __Future plugin goals:__
39
+
40
+ * Localization (translation support)
41
+ * HTTP header experimentation
42
+ * More features (based on user suggestions)
43
+ * Code maintenance/improvements
44
+
45
+ __Code inspired by:__
46
+
47
+ * [All 404 Redirect to Homepage](https://wordpress.org/plugins/all-404-redirect-to-homepage/)
48
+ * [404 Redirection](https://wordpress.org/plugins/404-redirection/)
49
+ * [Redirect 404 Error Page to Homepage](https://wordpress.org/plugins/redirect-404-error-page-to-homepage/)
50
+
51
+ NOTE: We released this plugin in response to our managed hosting clients asking for better access to their server environment, and our primary goal will likely remain supporting that purpose. Although we are 100% open to fielding requests from the WordPress community, we kindly ask that you consider all of the above mentioned goals before leaving reviews of this plugin, thanks!
52
+
53
+ == Installation ==
54
+
55
+ 1. Upload the plugin files to `/wp-content/plugins/404-to-homepage-littlebizzy`
56
+ 2. Activate the plugin through the 'Plugins' screen in WordPress
57
+ 3. Test the plugin is working correctly by loading a non-existent URL of your website
58
+
59
+ == Frequently Asked Questions ==
60
+
61
+ = Does this plugin alter my 404.php template? =
62
+
63
+ No, it automatically adds a 404 header using WordPress filters/hooks.
64
+
65
+ = How can I change this plugin's settings? =
66
+
67
+ This plugin does not have a settings page and is designed for speed and simplicity.
68
+
69
+ = I have a suggestion, how can I let you know? =
70
+
71
+ Please avoid leaving negative reviews in order to get a feature implemented. Instead, we kindly ask that you post your feedback on the wordpress.org support forums by tagging this plugin in your post. If needed, you may also contact our homepage.
72
+
73
+ == Screenshots ==
74
+
75
+ == Changelog ==
76
+
77
+ = 1.0.1 =
78
+ * fixed plugin meta data (description, etc)
79
+
80
+ = 1.0 =
81
+ * Initial release based on research of other plugins but using better code
82
+
83
+ == Other Notes ==
84
+
85
+ Some details about the plugin implementation:
86
+
87
+ This is a version that uses the early WP hook 'pre_handle_404' to avoid unnecessary code execution (posts types creation and taxonomies). But this hook is available only from WP 4.5.0, so the plugin declares also the 'wp' standard hook just in case for older versions.
88
+
89
+ Before to do the redirection to the homepage, there is a procedure that removes any existing previous header, so only 301 header will be sent in response to the HTTP request. You can see the headers in chrome from Network tab and checking preserve log. For Firefox you can use the Live HTTP headers addon, for example.