4

I'm researching the possibility of having the username set as a "subdomain" in Angular 2. From the research I've done so far it seems that for example a system hosted on IIS all that would be needed is URL rewriting to convert example www.mywebsite.com/username or www.mywebsite.com?user=username to www.username.mywebsite.com. (References: IIS URL Rewrite, Example Setup)

I might be wrong, but my thinking is that setting this up in IIS for an Angular 2 application wouldn't make a difference as when accessing routes the server is not being accessed - routing is handled by Angular itself.

So on to my question: Is there a way that this could be handled in an Angular 2 application?

Daniel Grima
  • 2,765
  • 7
  • 34
  • 58
  • I'm looking to the this too – duardbr Apr 09 '17 at 13:19
  • I'm searching this too :-/ – duardbr Nov 04 '17 at 09:31
  • @duardbr one way this could be done is by using wildcards on your server. For example your site would "accept" all subdomains i.e. `*.domain.com`. It's then up to you to validate the subdomain on the server. – Daniel Grima Nov 06 '17 at 08:43
  • Thanks Daniel, but its not clear yet ... in Rails (ROR) its very easy to implement. – duardbr Nov 07 '17 at 07:17
  • @DanielGrima can you be more clear, I have a similar thing to do, when user enter tenant1.mycompany.com, i should extract my tenant name from the url. How can i make this work in my local – Satya Ram Nov 16 '19 at 12:39
  • @SatyaRam I've added an answer to the question to highlight what can be done to extract the subdomain from the URL. If the answer helps you please accept the answer, of course if you have any questions I'd be happy to try to help :) – Daniel Grima Nov 16 '19 at 15:16
  • Hi Daniel Grima's can you refer to this question raised https://stackoverflow.com/q/58890401/11437467. I would be really glad if you could help me solve. – Satya Ram Nov 16 '19 at 18:21

1 Answers1

1

So I know I had asked this question a while ago, but I thought I'll describe what was done to go about this in an Angular application.

Firstly set up the subdomains on the server. In the Angular application to extract the subdomain you can have a function which using a regex extracts the subdomain.

Example

getAndSaveSubdomain() {
    let getSubdomainRegex = /(?:http[s]*\:\/\/)*(.*?)\.(?=[^\/]*\..{2,5})/i;
    let fullURL: string = window.location.host;
    let result: RegExpMatchArray = fullURL.match(getSubdomainRegex);
    let subdomain: string;

    if (result !== null) {
        subdomain = result[1];
        if (subdomain !== 'www') {
            localStorage.setItem('subdomain', subdomain);
        }
    }
}

For an explanation of the RegEx being used you can have a look at: https://regex101.com/r/OnkLD5/1

Daniel Grima
  • 2,765
  • 7
  • 34
  • 58