35

I couldn't find a straight answer to my question and need to know it from the real experts.

I had a website which urls were generated by Joomla. I believe that tons of urls are around in the search engines and I really don't know which of them all. A 302 redirect would be an option, but I can't say which urls need to be redirected.

The only thing I know that all the urls were generated by a sef404 script, it's a SEO script for Joomla.

My question, how can I make sure that all the orphan urls on google and other search engines are delivered correctly with a .htaccess file?

How do I 301 redirect all 404 pages to the homepage (root document)

At the moment I use a custom 404.html error file, but there are too many files and will give a rollercoaster of custom 404 error pages

2 Answers 2

50

I came up with the solution and posted it on my blog

http://web.archive.org/web/20130310123646/http://onlinemarketingexperts.com.au/2013/01/how-to-permanently-redirect-301-all-404-missing-pages-in-htaccess/

here is the htaccess code also

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . / [L,R=301]

but I posted other solutions on my blog too, it depends what you need really

3
  • 1
    this will 301 redirect ALL 404 pages to wherever you like, in the previous solution it is a 302
    – andrew
    Commented Jan 13, 2013 at 23:49
  • 3
    This should be the answer as the other answer is just a rewrite and not redirect +1 Commented Sep 3, 2015 at 9:40
  • 1
    For anyone who wants to know why this works as I did (httpd.apache.org/docs/2.2/mod/mod_rewrite.html): first line turns RewriteEngine on, second line sets a condition for the third line, ensuring that it's a non-existant file and passing the requested filename (using a server variable (%{})), third line matches everything, replaces it with the root folder, and sets two flags, the first (L) to stop any more rewrite rules from being applied, and the second (R=301) to force an external redirect using the error code 301.
    – Andrew
    Commented Oct 31, 2015 at 23:56
39

You will need to know something about the URLs, like do they have a specific directory or some query string element because you have to match for something. Otherwise you will have to redirect on the 404. If this is what is required then do something like this in your .htaccess:

ErrorDocument 404 /index.php

An error page redirect must be relative to root so you cannot use www.mydomain.com.

If you have a pattern to match too then use 301 instead of 302 because 301 is permanent and 302 is temporary. A 301 will get the old URLs removed from the search engines and the 302 will not.

Mod Rewrite Reference: http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html

1
  • 9
    Minus as this is not a redirect Commented Sep 3, 2015 at 9:40

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