Search code examples
reactjsreact-routersubdomainhosts

Modify `/etc/hosts` file and serve per-domain pages in React


React novice here. We have a situation in which we are going to be creating a sub-domain for every client that signs up for an app and then there will be a landing page for every client at <client_name>.ourwebsite.com.

The front-end for the website is going to be in React using React-Router v2.8, so when the user visits <client_name>.ourwebsite.com, the app needs to pick up the <client_name> and query the server for the data needed for landing page.

I am trying to set this up in the development environment by modifying the /etc/hosts file for every sub-domain name.

How do I get the React app to detect the <client_name> and get the data accordingly?


Solution

  • I hope this is a plain js question. You can get the hostname using window.location.host. This will return you xxx.ourwebsite.com as result in your case. Use this to get your subdoamin name and send it to api.