This typically involves configuring the DNS (Domain Name System) and some settings on Heroku. The following are the steps and examples:
1. Preparation
Ensure you have two Heroku applications, such as app1.herokuapp.com and app2.herokuapp.com. Additionally, ensure you have purchased a domain, such as example.com.
2. Configure the Root Domain
First, configure the root domain (example.com) to point to one of the Heroku applications (e.g., app1.herokuapp.com):
-
Add a Custom Domain to the Heroku Application:
- Log in to the Heroku Dashboard.
- Select your application (e.g.,
app1). - Navigate to "Settings" > "Domains and certificates" > "Add domain".
- Add your root domain (
example.com).
-
Configure DNS Provider:
- Log in to your domain registrar or DNS provider's control panel.
- Set up an
ALIASorANAMErecord (if supported by your DNS provider) pointing toapp1.herokuapp.com. If not supported, you can set multipleArecords pointing to the IP addresses provided by Heroku for your root domain.
3. Configure the Subdomain
Next, configure the subdomain (e.g., sub.example.com) to point to another Heroku application (e.g., app2.herokuapp.com):
-
Add a Custom Domain to Another Heroku Application:
- Repeat the above step 1, but this time add the domain
sub.example.comtoapp2.
- Repeat the above step 1, but this time add the domain
-
Configure DNS Provider:
- In the DNS settings, add a
CNAMErecord forsub.example.compointing toapp2.herokuapp.com.
- In the DNS settings, add a
Example
Assuming your DNS provider supports ALIAS records:
-
Root Domain (
example.com):- Type:
ALIAS - Name:
example.com - Value:
app1.herokuapp.com
- Type:
-
Subdomain (
sub.example.com):- Type:
CNAME - Name:
sub - Value:
app2.herokuapp.com
- Type:
Notes
- DNS changes may take some time (typically a few minutes to several hours) to propagate globally.
- Ensure you update the SSL certificate to support the newly added custom domains if HTTPS is enabled on Heroku.
By setting it up this way, you can achieve the root domain and subdomain pointing to different Heroku applications. This is useful for managing large projects and distributed service architectures.