1

Actually our website creating by using Java technologies so installed Tomcat,apache on VPS and deployed. we are creating sub domains for registered customers we have enabled the Wildcard by creating A record with *.mydomain name.

after that we have to handle sub domains in Apache... Once Apache is set up and we have to verify it works for random subdomains, have to write rewrite rules to map the subdomains to another URL (doesn't matter what lang we are using).

Hence UserName8328.domain.com would actually request something like domain.com/users/UserName8328 from your server.

So can you please suggest me how to do this ? For this question there is another link

This question discusion going this link with JAVA. Mean while we struck up with Apache related issue. can you help on this ? https://stackoverflow.com/questions/19188877/what-is-best-way-to-create-sub-domains-dynamically-by-using-java-whenever-user-r

  • 1
    Actually you can find most of the required information in Apache Web server documentation at http://httpd.apache.org/docs/current/mod/mod_rewrite.html. I am a nginx user so excuse me here... but your question is fairly easy. – kworr Oct 04 '13 at 20:47

1 Answers1

1

For the Apache side, my sites use:

httpd.conf

<VirtualHost 8.8.8.8:80>
    ServerName domain.com
    DocumentRoot /var/www/siteroot/
    ServerAlias *.domain.com www.domain.com
    VirtualDocumentRoot /var/www/siteroot/users/%1
</VirtualHost>

What this will do is have any subdomain load the same page as htttp://domain.com/users/subdomain. If you are using www.subdomain.com, you may have to use %2 instead of %1 above.

For the .htaccess side, I'd take a look at (this ServerFault question.)[Troubleshooting a .htaccess wildcard subdomain rewrite.

I'm not a Java guy, but the idea is similar. In PHP, I'd figure out what address they actually went to (like your initial StackExchange question), then use that to load the user's information and hence the page. So all subdomains for a site I'd create would be like:

  1. User requests htttp://userid.domain.com.
  2. Server sees *.domain.com goes to domain.com/users/?q=subdomain. The user doesn't see this - they still think they are at userid.domain.com.
  3. htttp://domain.com/users/index.php checks for the "q" query field - If found, it loads the content for that user.

The whole time the user will see htttp:// userid.domain.com even though the script is executing as htttp://domain.com/users/index.php. Essentially the script sees the subdomain as a query string variable. Alternatively you can push all users to htttp://domain.com/users/index.php and use Environmental Vars to find the subdomain.

I'd post a new StackExchange question, or rewrite your current one, once you verify that you are able to see htttp://domain.com/users/index.html for htttp://kdfsdkfsdklfsd.domain.com.

Mat Carlson
  • 111
  • 3
  • Thanks for giving suggestion. And one more point I would like to clarify.... For example If I click on url like this.. www.somenameee.wordpress.com It is pointing to Default error message page.If the user is not available. How to achieve that one... Should we do any changes in httpd.conf file or should we do in Java code ? – user2793170 Oct 06 '13 at 14:37
  • I'd handle it in a script. I'm assuming you'll use the same file and directory (on the file system) to handle all users. Most companies that do similar things redirect error messages to their main or help page. – Mat Carlson Oct 07 '13 at 20:55
  • On a side node, I'd make the www. optional - It's not fully accepted to require the 4th level subdomain. Sure, www.images.google.com works, but most people prefer to type in images.google.com instead (but both still work). – Mat Carlson Oct 07 '13 at 20:56