Cloudburg
Cloudburg

Reputation: 1

Apache2 subdomain redirects to main domain

I have an EC2 instance setup running Ubuntu 14.04 and Apache. I have a single elastic IP serving multiple domains and subdomains all of which point to individual folders on the server. The problem I am having is unless the subdomain is explicitly set in my .conf it will redirect to the main domain. I can't seem to find a definitive answer here or in google.

I have a single .conf file residing in /etc/apache2/sites-enabled/ serving all of the domains and subdomains like so:

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName mydomain.com
    ServerAlias www.mydomain.com
    DocumentRoot /var/www/html/mydomain.com
    <Directory /var/www/html/mydomain.com>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
    </Directory>
</VirtualHost>

<VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName sub1.mydomian.com
        ServerAlias www.sub1.mydomain.com
        DocumentRoot /var/www/html/sub1.com
        <Directory /var/www/html/sub1.com>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>
</VirtualHost>

<VirtualHost *:80>
            ServerAdmin [email protected]
            ServerName sub2.mydomian.com
            ServerAlias www.sub2.mydomain.com
            DocumentRoot /var/www/html/sub2.com
            <Directory /var/www/html/sub2.com>
                    Options Indexes FollowSymLinks MultiViews
                    AllowOverride All
                    Order allow,deny
                    allow from all
            </Directory>
    </VirtualHost>

<VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName mydomain2.com
        ServerAlias www.mydomain2.com
        DocumentRoot /var/www/html/mydomain2.com
        <Directory /var/www/html/mydomain2.com>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>
    </VirtualHost>

So if I go to sub1.mydomain.com or sub2.mydomain.com it gets properly routed. But if I type sub3.mydomain.com which does not exist in my .conf file it gets redirected to mydomain.com. I do not want this behavior. How do I resolve this?

Upvotes: 0

Views: 1135

Answers (1)

Kunal Pradhan
Kunal Pradhan

Reputation: 522

You need to enable name based virtualhosts. Add below line before your first virtual host and restart apache.

NameVirtualHost :80

Upvotes: 1

Related Questions