Adding a 404 redirection in WordPress can significantly improve your website’s usability, user experience, and SEO. If you have a WordPress website, you should create a 404 redirection in your website because:
- 404 Redirection Can Enhance User Experience
- Improves SEO
- Reduces Bounce Rate
- Fixes Broken Links
- Prevents Lost Conversions
- Make Your Website More Professional
In this article, we will introduce how to add 404 redirections to your WordPress site.
Method 1: Using a Plugin (Easy and Recommended)
-
Install the “All 404 Redirect to Homepage” Plugin:
- Go to your WordPress dashboard and navigate to Plugins > Add New.
- Search for All 404 Redirect to Homepage.
- Install and activate the plugin.
-
Configure the Plugin:
- After activation, go to Settings > All 404 Redirect to Homepage.
- Enable the plugin and choose the page where you want to redirect (e.g., Homepage or a Custom Page).
- Save the changes, and all 404 errors will be automatically redirected to the page you selected.
Method 2: Using Custom Code (Manual Approach)
You can also manually add code to your theme’s functions.php
file to redirect 404 errors.
-
Add Code to
functions.php
:- Go to your WordPress dashboard, navigate to Appearance > Theme File Editor.
- Open your theme’s
functions.php
file.
-
Add the Following Code:
function redirect_404_to_home() {
if (is_404()) {
wp_redirect(home_url());
exit();
}
}
add_action('template_redirect', 'redirect_404_to_home');
-
This code checks if a 404 page is encountered and redirects the user to the homepage.
-
Save the Changes:
- After adding the code, click Update File to save the changes.
Method 3: Using .htaccess (For Advanced Users)
If you have access to your server’s .htaccess
file and are comfortable with server configuration, you can create 404 redirects at the server level.
-
Access
.htaccess
:- Use an FTP client or your hosting control panel to access the
.htaccess
file in the root directory of your WordPress installation.
- Use an FTP client or your hosting control panel to access the
-
Add the Redirect Rule: Add the following rule at the bottom of your
.htaccess
file:
ErrorDocument 404 /index.php
-
This will redirect all 404 errors to your homepage or another page as specified by
/index.php
. -
Save the Changes:
- Save the
.htaccess
file and upload it back to the server.
- Save the
Method 4: Redirect to a Custom 404 Page
If you don’t want to redirect to the homepage but a custom 404 page, here’s how to do it:
-
Install a Plugin Like “404page”:
- Go to Plugins > Add New and search for the 404page plugin.
- Install and activate the plugin.
-
Create a Custom 404 Page:
- Go to Pages > Add New and create a custom 404 page with content you’d like to display.
-
Set the Custom Page:
- Go to Appearance > 404 Error Page.
- Select the custom 404 page you created.
This way, instead of an auto-redirect, users will see a customized 404 page.
Conclusion
- Plugins: Use All 404 Redirect to Homepage for easy setup.
- Custom Code: Add the code snippet to
functions.php
for manual control. - Server-Side: Use
.htaccess
for server-level redirection.