Apache2/mod_proxy for proxying requests.
by rp
I recently had to a task of integrating a third party flash app in one of our websites and the challenge was that this app which was embedded inside an iframe had to communicate with a javascript function on the parent frame. Which wasnt such a big problem to start off as it can be fixed by a workaround of the Same Origin policy by setting the document.domain property. You can read more about it here.
The real problem is when the link inside the iframe is coming from different domain (say abc.livedomain.com) and the parent document is on another domain (say xyz.dev.com), because the development/testing had to be done on the dev/qa servers and not on the livedomain.com.
Apache2′s module mod_proxy can in very handy in setting up this environment since we could easy fool the the browser by sending the right request headers. This is what the setup looked like.
Updated the local host(by updating the /etc/hosts) file or dns to make it livedomain.com point to my local apache instance
127.0.0.1 livedomain.com
Enabled virtual hosting on apache2 instance and added a new virtual host directive as follows (on OSX edit /etc/apache2/extras/http-vhosts.conf)
<VirtualHost *:80>
DocumentRoot "/Users/rp/websites"
ServerName localhost
ServerAlias livedomain.co
ProxyPass / http://xyz.dev.com
ProxyPassReverse / http://xyz.dev.com
TransferLog /Users/rp/websites/transfer.log
<Directory "/Users/rp/websites">
Allow from all
</Directory>
</VirtualHost>
restart apache
$ sudo apache2ctl restart
and now accessing livedomain.com, would send the request to apache which would internally forward the request to xyz.dev.com, the only thing to watch out for (like in case of using Drupal) if you are relying on $_SERVER['HTTP_HOST'] its best to check for $_SERVER['HTTP_X_FORWARDED_SERVER'] before going for the HTTP_POST as it wont properly without it. Here’s a bit of code that that explains it better.
if( isset( $_SERVER['HTTP_X_FORWARDED_SERVER'] )) {
$server = explode('.', rtrim($_SERVER['HTTP_X_FORWARDED_SERVER'], '.'));
} else {
$server = explode('.', rtrim($_SERVER['HTTP_HOST'], '.'));
}