• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1221
  • Last Modified:

How to rewrite errordocument 404's with domain variable (Apache 1.3)


For web server 'foo', I have in my httpd.conf

ErrorDocument 404 http://www.foo.com/404.html

Users will type 'http://www.foo.com/bogus_page.html', then Apache will redirect the request to 'http://www.foo.com/404.html'. But if users type 'http://www.bar.com/bogus_page.html', which has a DNS entry that resolves back to host 'foo', I want the ErrorDocument to redirect the request to 'http://www.bar.com/404.html" instead of "http://www.foo.com/404.html". Is there a way to do this?

There is some information on this from another document, as follows, but I'm not so sure this will work for us.



5. Redirecting to a 404 page if the directory and file do not exist

If your host doesn't provide for a "file not found" redirection, create it yourself!

RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule .? /404.php [L]

Here, -f matches an existing filename and -d matches an existing directory name. This script checks to see that the requested filename is not an existing filename or directory name before it redirects to the 404.php script. You can extend this script: include the URI in a query string by adding ?url=$1 immediately after the URI:

RewriteRule ^/?(.*)$ /404.php?url=$1 [L]

This way, your 404.php script can do something with the requested URL: display it in a message, send it in an email alert, perform a search, and so on.
1 Solution
slicer123Author Commented:
I have discovered a solution, see attached code snippet. Basically redirect the error document to a document on local server itself, then use rewrite cond and rule to capture the domain name and page, rewriting to appropriate destination server.
ErrorDocument 404 /bogus_page.html

RewriteCond %{HTTP_HOST} foo
RewriteRule bogus_page.html http://www.foo.com/404.html [L,R]

RewriteCond %{HTTP_HOST} bar
RewriteRule bogus_page.html http://www.bar.com/404.html [L,R]

Open in new window


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now