If you don't know what 404 error is, It is an error displayed on when a user requests a web page that couldn't be found on their server (i.e. the requested webpage is not available). In this case, If you are redirecting (302 redirects) to home page then it is confusing for end user. Definitely, you should avoid 302 redirecting for non-existent URL. Probably you don't want to send a low quality signal to Google and other engines if they are testing how 404's are being handled by requesting a noexist page. The best solution is to create custom 404 page having search box saying "Did you mean ...?"
In this post, we will see how to prevent 404 Redirect to Homepage in WordPress.
1. Permalink Settings
If you are getting 404 error for your regular posts and redirects to home page then change your Settings ยป Permalinks in your WordPress admin, Save Changes then again back to the setting you wanna keep and save it again. Sometimes, saving twice solve the problem.
2. Disable Plugins
If you are using any plugin to 404 redirect to Homepage, disable it. If you are using any SEO plugin, make sure you didn't set it in its configuration settings.
3. Check .htaccess
For non-wp site, it is generally used for 404 page
ErrorDocument 404 /index.php
You can check the defined page if it redirects to homepage.
By default, the .htaccess file in the root folder of your website has the content looks like below
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
If you find any redirect (R=301) in the rule or condition like below
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . / [L,R=301]
update it to prevent from redirect or keep the default WordPress settings.
4. 404.php
If your .htaccess file configuration is okay but still redirects to home page. Then open 404.php in your theme folder and check content
Make sure it is NOT redirecting like the below code:
<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".get_bloginfo('url'));
exit();
?>
5. Change Theme
WordPress provides is_404() method to check for 404 error. It might be possible your theme is redirecting it to home page. To verify this, change your theme.
6. NOBLOGREDIRECT Bug
In wp-config.php, NOBLOGREDIRECT is defined so that when someone enters a subdomain that does not exist on your site to redirect to whatever url you wish it to. But the bad thing is when someone tries to access the page of domain that doesn't exist, it redirects to NOBLOGREDIRECT defined location instead of throwing 404. To fix it, add following PHP code in functions.php or any related file
<?php
remove_action( 'template_redirect', 'maybe_redirect_404' );
?>
Conclusion
To redirect 404 page to Homepage(301/302) is not good for SEO. In WordPress, the standard themes provide 404.php page. You can customize it to make it more user friendly and display it if the requested page doesn't exist. In this troubleshooting guide, we have seen different steps to prevent 404 Page redirect to Homepage.
Hope, It helps.