Reputation: 2825
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?
Upvotes: 4
Views: 1261
Reputation: 2825
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
Upvotes: 1