Forum Moderators: phranque

Message Too Old, No Replies

Redirect when file not found

Using .htaccess to redirect only when a file is not found.

         

acidic

6:30 pm on May 18, 2004 (gmt 0)

10+ Year Member



He there.

I need to be redirect my visitors to another page when the file they are looking for is not found. At the moment I am using the following in an .htaccess file

RewriteEngine on
RewriteBase /mod
RewriteRule ^(.*) [mydomain.com...]

This unfortunately redirects even when the file is found. Is there any way to redirect only when a real file is not found? I can’t just use standard custom error 404 pages because I need to know the name of the non existent file.

Thanks a lot, any help would be much appreciated.

jdMorgan

6:52 pm on May 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See the -f option of RewriteCond [httpd.apache.org]

Also, be aware that if you *never* return a 404 error, then some search engine spiders may consider your site to be an "infinite URL space" and may limit the depth of their spidering.

Requests for pages which are not replaced by a newer, relevant page should return a 404. Spidering problems as noted above, and duplicate content issues may ensue if you redirect *all* missing pages to one page.

You may be able to modify your php code to get the requested page name from the request_uri or request_filename variable, and then you can use the standard ErrorDocument 404 directive with your script as the errordocument URL-path.

Jim

gergoe

7:27 pm on May 18, 2004 (gmt 0)

10+ Year Member



By using any of these methods to handle the 404 not found errors, it is recommended to set the status code of the response to 404 also because of the reasons Jim mentioned above. Check out this [php.net] part of the php documentation, it is about changing the "default" 200 status code to any other you need (404 in your case).

acidic

10:12 am on May 20, 2004 (gmt 0)

10+ Year Member



Thanks a lot for the help, I decided to use the php error page technique used by jdMorgan. Works like a charm.