WwwRedirect

View Source

A Plug for redirecting HTTP requests between www and non-www versions of domains.

This library redirects requests between www.example.com and example.com based on your preference.

Installation

If available in Hex, the package can be installed by adding www_redirect to your list of dependencies in mix.exs:

def deps do
  [
    {:www_redirect, "~> 0.1.0"}
  ]
end

Usage

# Redirect to non-www (default behavior)
# E.g. `www.example.com` -> `example.com`
plug WwwRedirect
plug WwwRedirect, to: :non_www

# Redirect to www
# E.g. `https://example.com/path` -> `https://www.example.com/path`
plug WwwRedirect, to: :www

Configuration Options

The plug accepts the following options:

  • :to - Specifies the redirect target. Can be :www (default) or :non_www.
    • :www - Redirects bare domains to www versions (e.g., example.comwww.example.com)
    • :non_www - Redirects www domains to bare versions (e.g., www.example.comexample.com)