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!

1 Like

While I have not being using such scenario, I think You might search for multi tenant.

This will give You…

3 Likes

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.

2 Likes

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.

2 Likes

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

3 Likes

Yes, because when I need to offer more functionality when running it locally. But I added the plug you said to make some things easier, thank you!