Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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.


share|improve this question

3 Answers 3

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

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

or just go home...

ErrorDocument 404 /


share|improve this answer
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 '10 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 '10 at 18:35

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.

share|improve this answer
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 '10 at 18:36
"absolute relative paths"? Absolute or relative, can't be both. –  John Gardeniers Sep 8 '10 at 21:23

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.

share|improve this answer

Your Answer


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