View Source CanonicalHost

Package Documentation CI

Plug for redirecting all traffic to a canonical host.

Installation

The package can be installed by adding canonical_host to your list of dependencies in mix.exs:

def deps do
  [
    {:canonical_host, "~> 0.2"}
  ]
end

Usage

# config/runtime.exs
config :canonical_host, :default, host: "https://myhost.com"
# plug pipeline in router.ex or endpoint.ex
plug CanonicalHost

Multiple canonical hosts

# config/runtime.exs
config :canonical_host, :one, host: "https://host-one.com"
config :canonical_host, :two, host: "https://host-two.com"
# one plug pipeline
plug CanonicalHost, config_key: :one

# another plug pipeline
plug CanonicalHost, config_key: :two

Alternatives

plug_canonical_host

I chose to create a new library instead of using this library for the following reasons:

  1. Has awkward configuration. It is not convenient to configure at runtime. Instead, the README suggests that you write your own function plug and then call both PlugCanonicalHost.init/1 and PlugCanonicalHost.call/2 in your function plug.
  2. Redirects HTTP requests with methods other than GET. Consider a case where an API client sends a POST request to your app at the non-canonical domain. PlugCanonicalHost will cause the request to not be handled but will return an HTTP 301 response which would make the client believe that the request succeeded.
  3. Tries to redirect with the same scheme (HTTP/HTTPS) and port number as the original request. I'd prefer to always redirect to the canonical scheme/host/port and save the user a possible extra redirect.