juanbono
Is there an idiomatic way of supporting multi domains?
Hi! We are coding a CMS like Wordpress and I’m trying to add support for multisite (i.e every site has a different domain) but I don’t know how to do it in an idiomatic way.
Also, the routes have to be different if I’m using it on my machine:
http://site-2.com:4000/category/2 points to the same page as http://localhost:4000/site/2/category/2 when running locally.
Right now I have duplicated my router code to handle the 2 different situations:
- When the request comes from localhost
- When the request comes from a different domain (e.g http://site-2.com/).
The downsides of my approach are that it has a lot of duplicated code and I need make several helper functions to handle each case every time I want to show a different route depending on the hostname.
Is there another way of doing it? Any comment would be welcomed.
Thank you!
Marked As Solved
amnu3387
But is there any limitation in using “yoursubdomain.lvh.me” for localhost? Wouldn’t this allow you to keep exactly the same routing structure?
Then you would only need a plug to read and set the correct domain/subdomain before the controller resolving the controller action
Also Liked
kokolegorille
While I have not being using such scenario, I think You might search for multi tenant.
This will give You…
axelson
Can you boil down the difference in the routing to a single change in routing that will always be different? Such as using site-number.com always becomes /site/number? If so, then you can programmatically script something. But if not then you might be better off with different routers since your routing is different.
mbuhot
Maybe try factoring the duplicated routes into a second router and forward from the main router to the site router.
The site id could be extracted from the path or host and stored in the Conn.assigns.
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
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









