Search code examples
javaspring-mvccloudsubdomain

how to create sub domains programmatically using Spring /Java?


I want to create multiple subdomains using Java/Spring on a cloud server. Do we need any specific API to create the subdomains?

Eg: Domain: www.example.com

Sub Domain: www.my1.example.com

Sub Domain: www.my2.example.com

Sub Domain: www.my3.example.com


Solution

  • It is a common mistake to think about subdomains in a Spring MVC project. The subdomains are a common thing of the servers such as Apache.

    If you want to create "subdomains" programatically you will need to map the URL using filters. Create a custom filter which parses whole url and extracts this called "subdomain".

    If you have my1.example.com in the filter you may extract the my1 part, and send it for a RequestMapping such as /{myXXX}/index If you don't want to show in the URL the /{myXXX}/index you may use also rewrite URL for achieving that.