Coralizing Your Site


I've been having a bit of fun with Coral, and found a couple of great uses for it. Unlike Melo, I don't intend to "coralize" my RSS feed just yet, but offloading the linked images to it to solve the Bloglines Mystery was pretty damn trivial:

RewriteCond %{HTTP_VIA} !CoralWebPrx
RewriteCond %{REQUEST_URI}  !^/images/* [NC]
# This matches both the base URL and the Coral alias
RewriteCond %{HTTP_REFERER} !^https?://(the\.)?taoofmac\.com(\.nyud\.net\:8090)?/ [NC]
RewriteCond %{HTTP_REFERER} !^$
# This should all go in one line:
RewriteRule \.(php|jpe?g|gif|bmp|png|css|js)$ \
http://the.taoofmac.com.nyud.net:8090%{REQUEST_URI} [NC,R,L]

The above makes sure that requests done by Coral itself are always allowed in, and shunts all requests for graphical content that is not referenced in my pages (or does not have a null referrer) to Coral. You must make sure you avoid confusing Coral by avoiding processing referrals from your Coral alias, though.

Since the trouble with Bloglines was that its JavaScript generated funky (i.e., invalid) referrers, Bloglines users will be glad to know they can use my standard RSS feed again - Coral will serve the inline images for me.

If you can't use .htaccess and mod_rewrite, you can also do pretty much the same using PHP alone:

  if( !preg_match( "/CoralWebPrx/", $_SERVER["HTTP_VIA"] ) ) {
    
header( "X-Redirect-Reason: Bandwidth Saving" );
    
header( "X-Cache-Service: Coral Distributed Content Network" );
    
header( "Location: http://the.taoofmac.com.nyud.net:8090" .
            
$_SERVER["REQUEST_URI"] );
    exit;
  }

(I like to add the extra headers so that I can debug this sort of thing properly with livehttpheaders. Interestingly enough, Coral seems to cache those as well).

Of course, this opens up extremely interesting possibilities. I could, for instance, put my entire site on Coral by simply putting a "*" in the RewriteRule above, or deal with likely slashdottings by changing the conditions a bit and diverting all Slashdot referrals to Coral instead of a lame static page (I've actually done this already inside the global PHP handler I use for referral filtering, but using .htaccess is more efficient).

(Update: The Flash plugin in Safari seems to have issues with Coral as well. My guess is that it sends out a bum referrer, something I can't trace easily.)

Another thing you can do if you redirect traffic to Coral is to add some code to let your users know what happened:

  if( isset( $_SERVER["HTTP_VIA"] ) &&
      
preg_match( "/CoralWebPrx/", $_SERVER["HTTP_VIA"] ) ) {
    echo
"This content is being served via Coral";
  }

This will print out the message on pages cached by Coral (I use a floating div myself).

It's a bit pointless doing anything more sophisticated, though, until Coral stops using port 8090 (which doesn't go through most corporate firewalls). But so far, it's perfectly useful for non-essential stuff that tends to waste bandwidth.


See Also: