Amanda Novaes Amanda Novaes - 2 years ago 69
Apache Configuration Question

Make 301 redirect when file does not exist in the server

I know I can use "ErrorDocument 404" to set a redirect when somefile does not exist anymore in my server BUT the server sends a 404 status to the browser. I want to make that redirect when the file does not exist using 301 status and using only my htaccess file.

So my question is this: if the user hits URL like and there is no such file "fldsjfds.php" in the server, how an I redirect the user to my MAIN PAGE using 301 redirect?

I dont want use 404 for very reasons, one of them is SEO, a programmer moved some pages of us to other names and I dont want to lose all that juice, I want to send a 301 redirect so google pass that PR along.

Answer Source

You can use this rule in site root .htaccess:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . / [L,R=301]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download