GamendWeb.Plugs.CanonicalHost (gamend_web v1.0.1215)

Copy Markdown View Source

Redirects every other hostname to the one canonical host.

www.example.com and example.com are different URLs to a crawler. Serving both with a 200 splits the ranking signals of every page across two addresses; a rel=canonical tag asks search engines to merge them, but only after they have fetched both, and it is only a hint.

Off unless :canonical_host is set, because the right answer is a host decision — some sites are canonically www. A host opts in with:

config :gamend_web, :canonical_host, "example.com"

What is deliberately not redirected

  • Anything but GET/HEAD. A 301 on a POST loses the body: browsers re-issue it as a GET, so a form submitted to the wrong host would silently do nothing.
  • The health endpoint, which container and uptime checks hit by IP or by an internal service name that will never be the canonical host.
  • ACME challenges, so a certificate for a redirected alias can still be issued — the challenge has to be answered on the alias itself.
  • Requests with no host, which is what an HTTP/1.0 client sends.

This runs at the endpoint rather than in the CDN so it holds however the site is fronted. A CDN rule is fine too; both together are harmless, since the second request already carries the canonical host and falls straight through.