Question

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.

Was it helpful?

Solution

This can not work in the way you want. If wordpress is on example.com then all the auto generated links will point to example.com even for pages that are on the CDN under the www.example.com domain. This will result that after the first page being served from the CDN many other pages will be served directly from wordpress.

  1. from inspecting the code, the code that does it is deep inside redirect_canonical so either you need to write your own redicrection and do something like

    remove_action("template_redirect", "redirect_canonical");

    add_action('template_redirect', 'wpsr12150_redirect_canonical');

    function wpsr12150_redirect_canonical() { // code without the www redirection }

but it is probably easier to handle it in an .htaccess file, for example make it always remove the www from incoming requests

  1. yes!

OTHER TIPS

I did manage to get this to work. Here are the details of what I did in case you want to do the same thing: https://gist.github.com/kingkool68/7421460

After two days searching I've finally found the answer for my problem

// disable WordPress's Canonical URL Redirect feature remove_filter('template_redirect','redirect_canonical');

from https://taylor.callsen.me/settings-up-aws-cloudfront-in-front-of-wordpress/

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top