You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Bernd Schönbach <be...@googlemail.com> on 2011/09/19 21:34:36 UTC

[users@httpd] mod_rewrite redirect to different server

Hi,

is it possible to write a mod_rewrite rule, which reroutes requests to
another server, if they are not found on the current server e.g.:

Requested URL:
www.foo.com/home.php

Backup URL:
www.bar.com/home.php

The redirect should only be made if the Requested URL delivers a 404
message.

I intend to use this for images etc. so I don't have to copy 20 GB of
data from one server to my localhost just to get all the image files.

Thanks
Bernd

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] mod_rewrite redirect to different server

Posted by Mark Montague <ma...@catseye.org>.
On September 19, 2011 15:34 , =?ISO-8859-15?Q?Bernd_Sch=F6nbach?= 
<be...@googlemail.com> wrote:
> is it possible to write a mod_rewrite rule, which reroutes requests to
> another server, if they are not found on the current server e.g.:
>
> Requested URL:
> www.foo.com/home.php
>
> Backup URL:
> www.bar.com/home.php
>
> The redirect should only be made if the Requested URL delivers a 404
> message.


You can do what you want using mod_rewrite:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.*) http://www.bar.com/$1 [R,L]
See https://httpd.apache.org/docs/2.2/rewrite/ for information on how to 
use mod_rewrite.

Alternatively, you could set up an ErrorDocument on your server to 
handle all 404 errors.  Have this ErrorDocument be a script that gets 
the URI path the user was trying to access from the REDIRECT_* 
environment variables and then prints HTTP response headers to redirect 
the user to the appropriate URL for www.bar.com.  For details, see 
https://httpd.apache.org/docs/2.2/custom-error.html

--
   Mark Montague
   mark@catseye.org


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org