Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a domain like "http://username.example.com" in a J2EE web application?

I am a web application developer, just a beginner. I am designing a small J2EE web application (for example, the service name would be like http://www.example.com). It uses Apache Tomcat. Spec: When a user sign up into the web application, he will get a custom domain like

http://username.example.com

How can i accomplish this in my web application. I am still developing the app. i have not hosted it yet.

like image 561
Anand Avatar asked Feb 06 '10 06:02

Anand


1 Answers

You’ll need to create a virtual host for each subdomain in Apache configuration like:

<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /www/subdomain1.example.com
ServerName subdomain1.example.com
ErrorLog logs/subdomain1.example.com-host.example.com-error_log
CustomLog logs/subdomain1.example.com-access_log common
</VirtualHost>

There is one problem though. If your solution requires folders creation, virtual host addition and apache restarts which does not look good to me. How would I deal with this problem is:

  1. Create a wildcard virtual host for “*.example.com” so that all requests to subdomains go to this virtual host.
  2. Setup a rewrite rule for it to redirect all requests to one page, say index.jsp
  3. Write up a JSP page taking the hostname the user came to, searching the redirect URL into a storage by it, i.e. mysql db, and redirecting to it. This script will be the index.jsp script on the wildcard host.

If you use this way, you only need to setup the above once and then add the subdomains in to the storage which appears to be more flexible solution than creating subfolders and modifying Apache configuration.

like image 110
Ashish Agarwal Avatar answered Nov 06 '22 02:11

Ashish Agarwal