Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

123
Views
Routing React Component based on hostname from the URL

I want to load React Component based on host name , Example , let say We have React application 'Frontend' and URL to access this app is <base_url>/test/login or <base_url>/test/home. Now I want to access this app from 2 different host name.

  1. test.abc.com/test/login , test.abc.com/test/home
  2. test.xyz.com/test/login , test.xyz.com/test/home

Now If user use #1 then it will show the login/home/logout page for test.abc.com only and for #2 login/home/logout page is different. Basically I need to customized the UI based o hostname.

Can anyone tell me , How can I achieve this scenario or is there any other way to do that?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

  1. Test the value of location.hostname
  2. Select your component based on that
  3. Pass it to your route

For example:

import { ABCLogin, XYZLogin } from "./LoginComponents";
const Login = location.hostname === "test.abc.com" ? ABCLogin : XYZLogin;

const App = () => {
    /* ... */
    <Route path="login" element={<Login />}>
    /* ... */
}

From a comment on another answer:

If the app is running from 100 hostnames and the login/home/logut and others are different based on host name , then in this case whether this approach is applicable too?

Going from 2 (as specified in the question) to 100 is something of a goalpost move, but…

Since including 100 different Login components would bloat the application, I would do the selection at build time. The specifics would depend on which toolchain you were using to build your production app.

For example, if you were using Webpack you could do something like:

resolve: {  
    alias: {
        components: path.resolve(__dirname, 'src/components/'),
        'components/Login': path.resolve(__dirname, `src/components/${process.env.TARGETHOSTNAME}Login`)
    }
}

This would require that you set the TARGETHOSTNAME environment variable before building.

about 4 years ago · Juan Pablo Isaza Report

0

Use window.location.hostname and useEffect to achieve desired behaviour. Basically check the url inside useEffect and redirect using ReactRouterDOM library or something similar.

about 4 years ago · Juan Pablo Isaza Report

0

get the hostname with the javascript function location.hostname; if the hostname is test.abc.com redirect to test.abc.com/logout else redirect to test.xyz.com/logout

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!