Apache url rewriting with masking

by jagbir on June 2, 2009

I got an assignment where I’ve to move some apps from a server (due to immense load) to different server without changing URLs. Main software app should remains in first server and all other smaller apps should be moved to another server but because all apps are integrated (for seamless login etc) with each other, URL on the browser should not change when browing the main app or any of its subordinate apps residing on different server.

The quick solution is to update httpd.conf in your main server to redirect traffic for certain apps to different server (or domain). Apache will work like a proxy when accessing other apps. for example, here are sample URLs:

Main Application: http://www.maindomain.com/
Pages of main application: http://www.maindomain.com/something.html
Other application 1: http://www.maindomain.com/wiki/
Other application 2: http://www.maindomain.com/forums/

Here’s what I’ve used, open httpd.conf and add following lines in it:

RewriteEngine on
RewriteRule ^/wiki/(.*) http://otherdomain.com/wiki/$1 [P,L]
RewriteRule ^/forums/(.*) http://otherdomain.com/forums/$1 [P,L]

ProxyPassReverse / http://otherdomain.com/

Pls make sure that mod_proxy and mod_rewrite are loaded in apache. In above lines:

line 1: Turns on rewrite engine provided by mod_rewrite
line 2 and 3: A rewrite rule to parse the url and detect the word (wiki) in begining of it. if word is there, then rewrite url using different domains (or IP address). The last parameter [P] indicates that its a proxy request.

line 4: URL that needs to be masked while browsing.

Save file, restart/reload httpd service and check. It worked in my case but your requirement may be different, I recommend having of a look of official documentation for url rewriting.

Previous post:

Next post: