4

I have an Apache running on an Ubuntu Server (14.04). I've configured the DNS A records and VH in order to have one domain and two subdomains just like this:

  • example.com
  • subdomain1.example.com
  • subdomain2.example.com

Everything is running ok, but the problem is that when I try to access to my website by the server ip, the browser shows subdomain1.example.com content instead of example.com content. This is not happening if I type example.com, in this case everything goes ok, but I'm worried if this could be a problem in the future or if I've done something wrong...

Here is my Apache VH config for example.com and subdomain1.example.com (subdomain2 is exactly like subdomain1):

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    ServerAdmin contact@example.com
    DocumentRoot /var/www/html

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined 
</VirtualHost>

<VirtualHost *:80>
    ServerName subdomain1.example.com
    ServerAdmin contact@example.com
    DocumentRoot /var/www/subdomain1

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined 
</VirtualHost>

Hope I've explained well... Thank you!

2
  • are all virtualhost configs in the same .conf file? May 4 '17 at 9:00
  • nope, they are in different .conf files, sorry for not mention it before
    – NeoSennin
    May 4 '17 at 10:06
3

From Apache docs

When a request is received, the server first maps it to the best matching based on the local IP address and port combination only. Non-wildcards have a higher precedence. If no match based on IP and port occurs at all, the "main" server configuration is used.

If multiple virtual hosts contain the best matching IP address and port, the server selects from these virtual hosts the best match based on the requested hostname. If no matching name-based virtual host is found, then the first listed virtual host that matched the IP address will be used. As a consequence, the first listed virtual host for a given IP address and port combination is the default virtual host for that IP and port combination.

This basicaly means that first VirtualHost encountered in httpd.conf will be used as default, or if all Virtual Hosts are in separate (included) .conf files (which is probably your case), then the one from alphabetically first .conf file will be used.

TLDR: try renaming the .conf file containing the example.com Virtual Host to aaa.conf (and restart httpd)

2
  • Good catch, this is almost certainly the issue. @NeoSennin, what you probably want to do is handle requests to the IP address differently. For that, create one more VirtualHost entry and store it in, say, 0-default.conf in the same directory as the others, or in your httpd.conf before the include line.
    – ghoti
    May 4 '17 at 10:21
  • Amazing! it was exactly that. Issue fixed, thanks so much!
    – NeoSennin
    May 4 '17 at 11:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.