ericteubert
Using :host parameter for matching routes
Hi,
My use case: I allow CNAMEs in my app, allowing users to bring their own hostname for public pages.
My idea then was to use host in all scopes for my native routes and a scope that matches any host for routes that may have custom hostnames.
scope "/", MyWeb, host: "my.example.com" do
pipe_through :public_browser
get "/", PageController, :index
end
scope "/", MyWeb, as: :hostname do
pipe_through :public_browser_custom_url
get "/*segments", CustomController, :index
end
This kind-of-works but it makes local development hard due to the static hostname matching. I first had host: Application.get_env(:my, MyWeb.Endpoint)[:url][:host] but the :host parameter only takes strings. (as an aside, it took a while to figure that out because Phoenix does not complain that you pass a function and the route just doesn’t match – but the documentation mentions that it’s string only, so I can’t complain
)
Any ideas how to make the :host parameter dynamic or is that not possible with the current implementation?
Marked As Solved
jayjun
You can match prefixes by setting a :host option that ends with ..
scope "/", MyWeb, host: "my.example." do
...
end
scope "/", MyWeb, as: :hostname do
...
end
will match my.example.com and my.example.local for non-custom hostname routes. Anything else falls through to the second scope.
Last Post!
AstonJ
A post was split to a new topic: How to match route when hostname is not known at compile time and simply matching by . is not possible?
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









