5

I am trying to setup my htaccess file such that all 404 pages are permanently redirected to the home/index page using 301. After I configure the htaccess file I try to test that it is working correctly by typing in a bogus URL such as site.com/asdf to see if the 404 will redirect. It does not, it just displays the page not found message from WordPress and fails to redirect to the homepage.

The code I am using (below) seemed to be working over on Stack Overflow (link, 2nd reply). I have even tried creating a PHP script to use with htaccess to redirect 404's, but still the same result, no redirection.

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . / [L,R=301]
# END WordPress
  • You need to place your additional code above your WordPress block (and remove the 2nd RewriteEngine directive) - the two sets of rules are conflicting. However, because they are conflicting it might break your WordPress rewrites? – MrWhite May 6 '14 at 20:21
  • From an SEO point of view, this is bad practice. The user should always be aware that they've hit a 404 error. – Liam McArthur Nov 9 '16 at 14:48
2

You could use: ErrorDocument 404 /index.php but word of caution... 404's are perfectly normal if the page no longer exists, for user experience you should only ever use a 301 redirect if the page that no longer exists is going to a equal page.. i.e about cars to cars, about rabbits to rabbits.

Using dozens to hundreds of 301's that are undesirable for your audience can result in a negative experience and Google dislikes this behavior. Only ever redirect a 404 page to a relevant page, otherwise leave the 404 or use 410 gone. Users from 404 pages can easily navigate to other pages should they want too, let them have the choice.

Unless there's thousands of similar urls you should use redirect 301 rather than rewrite, simply use:

redirect 301 /old-url/ http://www.yourdomain.com/new-url/

  • I understand the risks of using 301s. I need them in this particular circumstance. Yes I forgot to mention I have tried adding ErrorDocument 404 /index.php as well to HTaccess but it still does not redirect on bogus URLS. mod_rewrite is enabled so I am not sure whats going wrong. – user38415 May 6 '14 at 19:50
  • @user38415: Your 404 ErrorDocument directive didn't work for you because WordPress is already rewriting the URL when the requested file doesn't exist. – MrWhite May 6 '14 at 20:28
  • You need to look up for the solution in WordPress side. Since when using ErrorDocument directive, all 404 errors are generated by WordPress code. So, you need to find a way for WordPress to redirect to home page instead of displaying 404 page. Another alternative is to list all URLs separately using RewriteRules. – Tero Kilkanen May 7 '14 at 2:11
  • It is not a good idea to mix redirect and Rewrite directives in the same virtual server, since the interaction of these two modules can cause unexpected effects. – Tero Kilkanen May 7 '14 at 2:12
2

All you have to do is open your 404.php file in your theme’s folder. If it doesn’t exist, then create a blank php file. Paste the following code in there:

<?php 
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".get_bloginfo('url'));
exit();
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.