Just a plug in redirection at the top of the application endpoint.
In lib/app/endpoint.ex
:
defmodule App.Endpoint do use Phoenix.Endpoint, otp_app: :app socket "/socket", App.UserSocket plug App.Plugs.WWWRedirect
In lib/app/plugs/www_redirect.ex
:
defmodule App.Plugs.WWWRedirect do import Plug.Conn def init(options) do options end def call(conn, _options) do if bare_domain?(conn.host) do conn |> Phoenix.Controller.redirect(external: www_url(conn)) |> halt else conn
Please note that for this you need to restart the server in order to have an effect, since nothing in the lib
reboots .
source share