Cross domain AJAX with Apache’s mod_proxy

Apache LogoToday I sidetracked on a quest to set up some Javascript widgets to mainly play with animation and study CSS transitions.
Since I wanted to set up a picture slideshow, I decided to use Flickr’s API to load random pictures.

Now, for some unexplainable reason, I keep forgetting that AJAX calls cannot be made to other domains, so I was once again left to build a proxy script.
Being a good and lazy developer, I decided to look for an alternative, and I stumbled upon this gem: http://dwrbudr.com/#mod

It seems that Apache has a simple proxy built in that I didn’t know about.
Of course, a scripted proxy offers many advantages, like caching or modifying output.
However, when you only care for the raw API output, this is a simple, brainless lifesaver.

To get the thing to work on Ubuntu 10.10, I had to modify the .htaccess rules a bit.

RewriteEngine on
RewriteRule ^rest\/flickr\/(.*)$ http://api.flickr.com/services/rest/$1 [P]

Also, mod_proxy isn’t enabled by default. You can enable it like so:

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo /etc/init.d/apache2 restart

After this, you can throw your requests at /rest/flickr on your local domain without worrying about security restrictions!

Performance Optimization WordPress Plugins by W3 EDGE