I want to run my entire WordPress site through a CDN to improve performance (HTML, not just static CSS/JS/media). Lets say the origin server is example.com and www.example.com is the URL where traffic will go through the CDN set-up as an origin-pull.
The problem is an infinite loop will be created when visiting www.example.com which passes the request to WordPress which will look at the request and say "Actually this should be www.example.com according to the home_url and site_url defined in the Settings" Sooo...
1) Is there an easy way make WordPress stop redirecting to www under a certain condition (like checking for the x-forwarded-for headers which will be added to requests coming through the CDN)?
2) Am I going to have to set a constant to change the site URL for requests from the CDN and then set-up some sort of output buffering callback to replace http://example.com to http://www.example.com ?
I know there are plugins out there that handle this. I'm looking to roll my own so I can figure out how this all works. Any guidance would be much appreciated.