Q:

How to get a custom login page based on a different DNS?

Setup:
I have one web application that serves multiple organizations. Each organization has their own logos, color schemes etc that I retrieve from a database once they log in.
Each organization also has their own subdomain to get to my app. So I have example.com. Org1 would get to my login screen by going to org1.example.com.
I want to be able to provide custom logos, styling etc based on that subdomain but am not sure how that would be done.
Thanks! 

A:

There are many ways to do that, depending on the language, application server and operating system you are using.
Most of the time, people choose to have a new instance of the app running for each domain name, so that the application server knows to which instance each request must be mapped. Therefore, in such a case, you have nothing to program to handle this. Each instance is configured with specific logos, conf files, database connection, etc.
But of course, you can use only one instance of you app and use programming tips to know which domain is targeted by each request. This is less common, but it can have some advantages, for instance if you want to share some informations among the different domains you serve: you may want to send common informations to the different clients, and do not want to load them multiple times from a database, simply because you do not want to pay for the overhead of sharing informations through an external component (for performances reasons, for instance). So you can maintain those informations in the memory of your unique application. Memory-based distributed databases exist (memcached for instance), but it would never be as fast as using a single app with a single memory space.
Here are the most common ways to do that:

configure a distinct IP address for each server: let org1.example.com be resolved as 1.2.3.4 and org2.example.com as 1.2.3.5, then modify your app to bind a server socket on 1.2.3.4 (port 80) and another socket on 1.2.3.5 (same port). Then, in your main loop, when you accept a connection on the first socket, you know that org1 is the target, and when you accept a connection on the other socket, org2 is the target.
Here are the C-bindings for such a main loop: you use bind() on each socket to bind it to a specific local address, then you use select() to wait for a connection on one of those sockets, and when select() returns, you check the socket that has a pending connection in the listen queue to know which domain was the target (and after that, you call accept() on this socket to get a new communication socket specifically for this connection);
If you are using a web or application server, like Apache, Nginx, Tomcat, JBoss, etc., this programming scheme is already developed inside the server, so it is only a matter of configuration, for you.
configure the same IP address and check the Host: header, that comes with HTTP/1.1. This way, you know which domain was the target. You may configure the server to do the job and call distinct scripts, applications or pages depending on the target. But you can configure your server not to do that, and implement your custom behaviour inside your app. For instance, using Java with a Servlet container, you could simply call the following standard servlet API: request.getHeader("Host") (request is an instance of javax.servlet.http.HttpServletRequest that the server gives you when calling some of your servlet callback functions).

Note: if you are using encrypted connections over SSL/TLS, there is a host name at the SSL/TLS network layer, in the SNI optional field (Server Name Indication). This information should be exactly the same as the one contained in the HTTP Host header. But some web clients do not set the same value for those two placeholders. This is called domain fronting. A lot of application servers do no check that those two values are the same. So, you should check it by yourself, programmatically, if you do not want to be hacked by some clients like unfair robots (domain fronting is also used by fair applications, like Telegram, but this is another story).

