PlugCanonicalHost
PlugCanonicalHost ensures that all requests are served by a single canonical
host. It will redirect all requests from non-canonical hosts to the canonical
one.
Installation
Add plug_canonical_host to the deps function in your project’s mix.exs file:
defp deps do
[
…,
{:plug_canonical_host, "~> 0.3"}
]
end
Then run mix do deps.get, deps.compile inside your project’s directory.
Usage
PlugCanonicalHost can be used just as any other plugs. Add PlugCanonicalHost
before all of the other plugs you want to happen after successful redirection
to your canonical host.
defmodule Endpoint do
plug PlugCanonicalHost, canonical_host: "www.example.com"
end
For example, if your application is accessible via both example.com and
www.example.com, all traffic coming through example.com will be redirected
(with a 301 HTTP status) to the matching wwww.example.com URL.
$ curl -sI "http://example.com/foo?bar=1"
#> HTTP/1.1 301 Moved Permanently
#> Location: http://www.example.com/foo?bar=1
License
PlugCanonicalHost is © 2016-2018 Rémi Prévost and may be
freely distributed under the MIT license. See the
LICENSE.md file for more information.