Make WordPress Core

Opened 8 years ago

Last modified 5 years ago

#34800 new defect (bug)

Canonical 301 redirects go to request origin instead of site_url

Reported by: sevcsik's profile sevcsik Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.3.1
Component: Canonical Keywords: reporter-feedback
Focuses: Cc:

Description

I have a problem with canonical redirect using a CDN setup.

Let's say we have an origin server at origin.example.com, and the CDN at www.example.com.

When I request https://www.example.com/page-x, it redirects me to https://origin.example.com/page-x/ instead of https://www.example.com/page-x/, altough both site_url and home_url are set to https://www.example.com.

I suspect that Wordpress doesn't use site_url when composing the redirect URL, it's just using the request URL with a trailing slash added (which is in fact https://origin.example.com/page-x as the CDN translates the request).

The expected behaviour would be to either:

  • use the correct absolute URL in the header, like Location: <site_url> + <canonical_path>
  • use a relative URL, like Location: <canonical_path>

I also checked the Redirection plugin, which returns relative URLs in the 301 redirects, and those work fine with my setup.

Thanks for looking into this!
Sevcsik

Attachments (2)

canonical.php.diff (259 bytes) - added by sevcsik 8 years ago.
canonical.php to use site_url host instead of original host
canonical.php.2.diff (1.4 KB) - added by sevcsik 8 years ago.
canonical.php to use site_url host instead of original host (git diff version)

Download all attachments as: .zip

Change History (5)

#1 @johnbillion
8 years ago

  • Keywords reporter-feedback added
  • Summary changed from Wordpress canonical 301 redirects go to request origin instead of site_url to Canonical 301 redirects go to request origin instead of site_url

Thanks for the ticket @sevcsik.

Can you explain why your origin server serves its site from origin.example.com when its home and site_url are set to www.example.com? What's the reason that the CDN requests origin.example.com instead of www.example.com? This seems like a strange configuration.

Normally with a reverse proxy setup such as this, the origin is served from the same host as the CDN. Is there a reason your site is setup otherwise? Any more info you could provide will help us look into this issue.

#2 @sevcsik
8 years ago

@johnbillion, thanks for the response.

According to CloudFront docs [1], the requests to the origin server are sent with a host "set the value to the domain name of the origin that is associated with the requested object." Which is in line of my general (albeit maybe shallow) understanding of how CDNs work.

Regardless of my setup, I think using site_url() would be more consistent with the way WordPress handles absolute URLs in general. Template functions, such as get_stylesheet_directory_uri, or the login redirection in wp-admin works the same way. I couldn't come up any other examples when host is used for assembling absolute URLs rather than site_url().

I attached a diff which solves the issue, although I don't know if it would break something else.

Cheers

[1]: http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/RequestAndResponseBehaviorCustomOrigin.html#request-custom-headers-behavior

@sevcsik
8 years ago

canonical.php to use site_url host instead of original host

@sevcsik
8 years ago

canonical.php to use site_url host instead of original host (git diff version)

#3 @sevcsik
8 years ago

Ignore the first two diffs - I realised my test setup was wrong, and in fact it causes infinite redirects.

Note: See TracTickets for help on using tickets.