It took me a lot of RnD to finally come up with a solution that I definitely looked at. Most of the answers are given partially corrert. However, this requirement was very typical, and I solved it as follows.
I developed a servlet that checks hostnames (valid hostnames are stored in the database) and valid url patterns and does the appropriate routing. (Server side routing). This also includes checking how further routing occurs, for example, if someone visits www.porta1.com and clicks a link to it, he must land at www.portal1.com/aboutus.
I know a lot of hard work, but it works exactly what you need.
source share