Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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
share|improve this question
    
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? –  w3d May 6 at 20:21

1 Answer 1

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/

share|improve this answer
    
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 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. –  w3d May 6 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 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 at 2:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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