{"id":15491626,"url":"https://github.com/remi/plug_canonical_host","last_synced_at":"2026-04-09T05:31:56.437Z","repository":{"id":45318787,"uuid":"49435065","full_name":"remi/plug_canonical_host","owner":"remi","description":"PlugCanonicalHost ensures that all requests are served by a single canonical host.","archived":false,"fork":false,"pushed_at":"2023-10-03T20:45:46.000Z","size":120,"stargazers_count":40,"open_issues_count":0,"forks_count":8,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-04-02T04:29:02.949Z","etag":null,"topics":["elixir","http","plug"],"latest_commit_sha":null,"homepage":"https://hex.pm/packages/plug_canonical_host","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/remi.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2016-01-11T15:26:05.000Z","updated_at":"2025-09-02T22:44:18.000Z","dependencies_parsed_at":"2023-02-13T22:31:31.562Z","dependency_job_id":"8e0b554c-8e3b-4d54-b8df-f87fc2628158","html_url":"https://github.com/remi/plug_canonical_host","commit_stats":{"total_commits":116,"total_committers":7,"mean_commits":"16.571428571428573","dds":"0.051724137931034475","last_synced_commit":"332f8eb8209758700872087fdf7958c7982900fd"},"previous_names":["remiprev/plug_canonical_host"],"tags_count":18,"template":false,"template_full_name":null,"purl":"pkg:github/remi/plug_canonical_host","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/remi%2Fplug_canonical_host","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/remi%2Fplug_canonical_host/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/remi%2Fplug_canonical_host/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/remi%2Fplug_canonical_host/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/remi","download_url":"https://codeload.github.com/remi/plug_canonical_host/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/remi%2Fplug_canonical_host/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31587782,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-08T14:31:17.711Z","status":"online","status_checked_at":"2026-04-09T02:00:06.848Z","response_time":112,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["elixir","http","plug"],"created_at":"2024-10-02T07:54:41.277Z","updated_at":"2026-04-09T05:31:56.420Z","avatar_url":"https://github.com/remi.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://user-images.githubusercontent.com/11348/56806359-b6a07f00-67f9-11e9-96bd-6a456a96880c.png\" width=\"600\" /\u003e\n  \u003cbr /\u003e\u003cbr /\u003e\n  \u003ccode\u003ePlugCanonicalHost\u003c/code\u003e ensures that all requests are served by a single canonical host.\u003cbr /\u003e It will redirect all requests from non-canonical hosts to the canonical one.\n  \u003cbr /\u003e\u003cbr /\u003e\n  \u003ca href=\"https://github.com/remi/plug_canonical_host/actions?query=workflow%3ACI+branch%3Amaster+event%3Apush\"\u003e\u003cimg src=\"https://github.com/remi/plug_canonical_host/workflows/CI/badge.svg?branch=master\u0026event=push\" /\u003e\u003c/a\u003e\n  \u003ca href=\"https://hex.pm/packages/plug_canonical_host\"\u003e\u003cimg src=\"https://img.shields.io/hexpm/v/plug_canonical_host.svg\" /\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n## Installation\n\nAdd `plug_canonical_host` to the `deps` function in your project’s `mix.exs` file:\n\n```elixir\ndefp deps do\n  [\n    …,\n    {:plug_canonical_host, \"~\u003e 2.0\"}\n  ]\nend\n```\n\nThen run `mix do deps.get, deps.compile` inside your project’s directory.\n\n## Usage\n\n`PlugCanonicalHost` can be used just as any other plugs. Add `PlugCanonicalHost` before all of the other plugs you want to happen after successful redirect to your canonical host.\n\nThe recommended way to define a canonical host is with an environment variable.\n\n```elixir\n# config/releases.exs\nconfig :my_app,\n  canonical_host: System.get_env(\"CANONICAL_HOST\")\n\n# lib/my_app/endpoint.ex\ndefmodule MyApp.Endpoint do\n  plug(:canonical_host)\n\n  defp canonical_host(conn, _opts) do\n    :my_app\n    |\u003e Application.get_env(:canonical_host)\n    |\u003e case do\n      host when is_binary(host) -\u003e\n        opts = PlugCanonicalHost.init(canonical_host: host)\n        PlugCanonicalHost.call(conn, opts)\n\n      _ -\u003e\n        conn\n    end\n  end\nend\n```\n\nFor example, if your `CANONICAL_HOST` is `www.example.com` but 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 `www.example.com` URL.\n\n```bash\n$ curl -sI \"http://example.com/foo?bar=1\"\n#\u003e HTTP/1.1 301 Moved Permanently\n#\u003e Location: http://www.example.com/foo?bar=1\n```\n\n### Allow multiple hosts\n\nIf you want to support _multiple_ hosts, you can make them skip the canonical host behavior but you’re still going to have to specify a canonical host for unknown hosts redirects.\n\n```elixir\n# config/releases.exs\nconfig :my_app,\n  canonical_host: System.get_env(\"CANONICAL_HOST\"),\n  passthrough_hosts: String.split(System.get_env(\"PASSTHROUGH_HOSTS\"), \",\")\n\n# lib/my_app/endpoint.ex\ndefmodule MyApp.Endpoint do\n  plug(:canonical_host)\n\n  defp canonical_host(%Plug.Conn{host: host} = conn, _opts) do\n    canonical_host = Application.get_env(:app, :canonical_host)\n    passthrough_hosts = Application.get_env(:my_app, :passthrough_hosts)\n\n    cond do\n      host in passthrough_hosts -\u003e\n        conn\n      is_binary(canonical_host) -\u003e\n        opts = PlugCanonicalHost.init(canonical_host: canonical_host)\n        PlugCanonicalHost.call(conn, opts)\n      true -\u003e\n        conn\n    end\n  end\nend\n```\n\nLet’s say we have `CANONICAL_HOST=www.example.com` and `PASSTHROUGH_HOSTS=foo.example.com,bar.example.com`.\n\nNow, all requests going through `www.example.com`, `foo.example.com` or `bar.example.com` path will skip the canonical host redirect behavior. Other hosts will redirect to `www.example.com`.\n\n```bash\n$ curl -sI \"http://example.com/foo?bar=1\"\n#\u003e HTTP/1.1 301 Moved Permanently\n#\u003e Location: http://www.example.com/foo?bar=1\n\n$ curl -sI \"http://foo.example.com/foo?bar=1\"\n#\u003e HTTP/1.1 200 OK\n\n$ curl -sI \"http://bar.example.com/foo?bar=1\"\n#\u003e HTTP/1.1 200 OK\n\n$ curl -sI \"http://www.example.com/foo?bar=1\"\n#\u003e HTTP/1.1 200 OK\n```\n\n### Exclude certain requests\n\nIf you want to _exclude_ certain requests from redirecting to the canonical host, you can use simple pattern matching in your function arguments:\n\n```elixir\n# config/releases.exs\nconfig :my_app,\n  canonical_host: System.get_env(\"CANONICAL_HOST\")\n\n# lib/my_app/endpoint.ex\ndefmodule MyApp.Endpoint do\n  plug(:canonical_host)\n\n  defp canonical_host(%Plug.Conn{request_path: \"/ignore-me\"} = conn, _opts) do\n    Plug.Conn.send_resp(conn, 200, \"👋\")\n  end\n\n  defp canonical_host(conn, _opts) do\n    :my_app\n    |\u003e Application.get_env(:canonical_host)\n    |\u003e case do\n      host when is_binary(host) -\u003e\n        opts = PlugCanonicalHost.init(canonical_host: host)\n        PlugCanonicalHost.call(conn, opts)\n\n      _ -\u003e\n        conn\n    end\n  end\nend\n```\n\nNow, all requests going to the `/ignore-me` path will skip the canonical host redirect behavior.\n\n```bash\n$ curl -sI \"http://example.com/foo?bar=1\"\n#\u003e HTTP/1.1 301 Moved Permanently\n#\u003e Location: http://www.example.com/foo?bar=1\n\n$ curl -sI \"http://example.com/ignore-me\"\n#\u003e HTTP/1.1 200 OK\n```\n\n## License\n\n`PlugCanonicalHost` is © 2016-2020 [Rémi Prévost](http://exomel.com) and may be freely distributed under the [MIT license](https://github.com/remi/plug_canonical_host/blob/master/LICENSE.md). See the `LICENSE.md` file for more information.\n\nThe plug logo is based on [this lovely icon by Vectors Market](https://thenounproject.com/term/usb-plug/298582), from The Noun Project. Used under a [Creative Commons BY 3.0](http://creativecommons.org/licenses/by/3.0/) license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fremi%2Fplug_canonical_host","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fremi%2Fplug_canonical_host","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fremi%2Fplug_canonical_host/lists"}