Wednesday, September 21, 2022

[FIXED] How to use two subdomain with Apache?

Issue

I have domain on name.com and I created two subdomain from DNS record

Type, Host,                   Answer,               TTL
A,    subdomain1.domain.com , EC2 IP,               300
A,    subdomain2.domain.com , EC2 IP same as above, 300


<VirtualHost *:80>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
        ServerName subdmain1.domain.com
        ServerAlias www.subdmain1.domain.com

        Project related Configuration....

</VirtualHost>
<VirtualHost *:80>
        ServerName subdomain2.domain.com
        ServerAlias www.subdomain2.domain.com
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
        Second project code ....
</VirtualHost>

I did above configuration in apache2 of my EC2 server but When I access any subdomain that automatic always goes to first VirtualHost and second Virtualhost not working.

please Help me for configure both subdomain on ec2

Note: I'm using main domain.com on name.com website


Solution

May be you should remove '*' from virtualhost and provide respactive domain name and add domain name in your allowedhost in respactive settings file, it worked for me.

<VirtualHost subdmain1.domain.com:80>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    ServerName subdmain1.domain.com
    ServerAlias www.subdmain1.domain.com
    Project related Configuration....
</VirtualHost>

and

<VirtualHost subdomain2.domain.com:80>
    ServerName subdomain2.domain.com
    ServerAlias www.subdomain2.domain.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    Second project code ...
</VirtualHost>


Answered By - Parth Modi
Answer Checked By - David Goodson (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.