I have just managed to get my site running behind a Cloudfront distribution using the custom distribution API, but now i cannot login to the site using the standard old address (since i screwed around with DNS). The way i have it hooked up is as follows:

backendblog - real server blog - cloud front distribution told to get data from backendblog

visiting blog results in the page being returned (and quite fast too!) but none of the dynamic stuff (search, login, etc) work. comments work grand since they are managed by Disqus.

So, i have 2 questions around this:

1: how do i set it so that backendblog will allow me to login without redirecting to blog (if i set the config, it changes all the URLs... want the URLs to be what ever site the page loads on...) 2: how do i configure pages to have higher expire times so they stay in CloudFront longer?

Any ever try this?

the site, for reference, is blog.lotas-smartman.net (clodfront) or backendblog.lotas-smartman.net (dedicated box in Germany). have seen Pingdom avg response times drop massively in the last week with cloudfront...

  • Have you tried the WP_SITEURL option in wp-config.php? – AutoBlogged Apr 8 '11 at 3:29
  • looking at my wp-config, there is no mention of WP_SITEURL. seems that, looking at stuff online, that WP_CONFIG is the opposite of what i want to do... i would like multiple domains pointing to one WP instance, and each of them to use the domain they came from... – TiernanO Apr 8 '11 at 7:58

I've been working on this exact same issue. What you need to do is have two domains. One for your backendblog and one that pointing at cloudfront. Your backendblog will be configured to serve up as itself (backendblog). In my test example I'm using my blog as the backendblog.

If this was for production use you would probably have content.example.com as backendblog and www.example.com as cloudfront. Then you setup cloudfront to pull from backendblog (you got this part figured out already) in my example.

The tricky part is to write a plugin that rewrites all your urls from backendblog to the cloudfront url when it's getting a request with the cloudfront user agent

Plugin Name: CDN Rewrite
Plugin URI: http://blog.andrewshell.org/cdnrewrite
Description: Rewrite urls on page if behind a CDN
Author: Andrew Shell
Version: 1.0.0
Author URI: http://blog.andrewshell.org/

if (0 == strcmp($_SERVER['HTTP_USER_AGENT'], 'Amazon CloudFront')) {
  $now     = time();
  $expires = strtotime('+1 hour', $now);
  header("Expires: " . gmdate(DATE_RSS,$expires) . "\n");

  add_filter( 'home_url', 'cdnrewrite_url', 100, 4 );
  add_filter( 'site_url', 'cdnrewrite_url', 100, 4 );

function cdnrewrite_url( $url, $path, $orig_scheme, $blog_id )
  return str_replace('blog.andrewshell.org', 'd11b8ym2xu437t.cloudfront.net', $url);

When you want to edit posts and such just log into backendblog, it shouldn't redirect because it's configured as backendblog (not the cloudfront url) yet all requests from cloudfront will be corrected so links stay on the CDN domain.

This isn't perfect, if you look in the source of my cloudfront url you will see a few references to my backendblog, but this is the line of thinking you'll want to go down. The next step is probably changing the plugin to capture all output in an output buffer and do a str_replace on it before returning it.


You mentioned that you want your urls changed to be what ever site the page loads on.

Maybe you could hook into pre_option_siteurl and pre_option_home and dynamically return the url you come from.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.