Tuesday, December 29, 2009

.htaccess ErrorDocument does not Catch 404 Error from a Non-Browser Call to the Site.

I came across this problem recently when trying to click on hyperlinks in a WORD document. The link used the following code in a .htaccess file.

ErrorDocument 404 /error_file.php


It worked when accessing the link through a browser, but WORD makes a call to the site first to determine if the page exists. For some reason, the file would not redirect, and WORD would say that it could not access the site. The same thing would happen when trying to access the page via PHP cURL or a W3C validator.

Thanks to the following page, I indirectly got the solution.
http://www.litespeedtech.com/support/forum/archive/index.php/t-1051.html
The page is actually discussing .htaccess files nested in subfolders, but the following code, which is mentioned in the posts, works better for redirecting from WORD.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /error_file.php [L]
</ifModule>