ericteubert

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 :slight_smile: )

Any ideas how to make the :host parameter dynamic or is that not possible with the current implementation?

Marked As Solved

jayjun

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!

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement