Tuesday, October 24, 2006

Apache Rewrite

A tricky situation. Support you have two webservers but there is only 1 public IP on your network. You want everybody browse the contents of the servers with different url (say server1.test.com for server1 and server2.test.com for server2).

If the content are in the same server, it would be easy to archieve this by using VirtualHost. So how about in this case? We can use the module "Rewrite".

Support the default webserver is server1 (all traffic from Internet will go to this server first). We add a virtual host and use rewrite to "redirect" request of server2:

...
<VirtualHost *:80>
ServerName :80
ServerAdmin admin@
ErrorLog logs/<hostname/IP>-error_log
CustomLog logs/<hostname/IP>-access_log combined

RewriteEngine on
RewriteRule ^/(.*) http://<hostname/IP>/$1 [L,P]
</VirtualHost>
...

Note:
hostname of server2 should be resolved by server1
you cannot have multitple SSL(443) connection with 1 IP

Reference: http://httpd.apache.org/docs/2.2/misc/rewriteguide.html