0

How can I write htaccess to do the following:

redirect ALL dead links on www.mysite.com to www.mysite.com (homepage)

Obviously I only want the dead links redirected.

Thanks

3 Answers 3

9

Just redirect 404 (missing) files to a path you want

ErrorDocument 404 http://www.somesite.gov/

or just go home...

ErrorDocument 404 /

http://www.bignosebird.com/apache/a7.shtml

2
  • 1
    See KarmaWhore's note about the difference between the two ways of using ErrorDocument. Also note that a custom error document that links to an internal search page (perhaps with some pre-filled context-sensitive magic based on the original URL?) is sometimes preferable to a blind redirect to the home page, at least from a user's standpoint :-)
    – voretaq7
    Sep 8, 2010 at 18:31
  • Good point. I figured cashbagg could use that as a starting point - showing him you can obviously stay within your own site but even go somewhere else. A site-map or search page is definitely a better way to go.
    – Matt
    Sep 8, 2010 at 18:35
4

remember that ErrorDocument 404 http://www.somesite.gov/ will actually hand the search engines a 302 or temporary redirect and won't remove those pages from the search engines.

ErrorDocument 404 / is probably more correct as it will generate a 404, but will send the person to the homepage. Any graphics and links should have absolute relative paths or you should put a <base href="/"> to make sure everything displays properly.

2
  • Good point. Was mainly trying to show you can stay in your site, or even push them somewhere else. Although most people would want to keep a user once they have brought them in. :)
    – Matt
    Sep 8, 2010 at 18:36
  • "absolute relative paths"? Absolute or relative, can't be both. Sep 8, 2010 at 21:23
0

This should also do it, and pass the failed page as an argument:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d RewriteRule (.*) index.php?badrequest=$1

I haven't tested it mind.

You must log in to answer this question.