admin管理员组

文章数量:1335436

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 and www.example 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 which passes the request to WordPress which will look at the request and say "Actually this should be www.example 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 to ?

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.

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 and www.example 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 which passes the request to WordPress which will look at the request and say "Actually this should be www.example 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 to http://www.example ?

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.

Share Improve this question asked Nov 6, 2013 at 23:15 kingkool68kingkool68 1,2191 gold badge11 silver badges24 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 1

This can not work in the way you want. If wordpress is on example then all the auto generated links will point to example even for pages that are on the CDN under the www.example 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!

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/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/

本文标签: urlsPreventing Canonical Redirect for CDN