What I'm trying to do is to show a page when a domain is visited but if someone tries to visit directly by ip (and not using the domain) i want to show him another page.

Classic name based apache hosting right?

Well my only question is what should i put in ServerName and/or ServerAlias?

What i've done till now is to uncomment this line:

NameVirtualHost *:80

and i've setup 2 virtual hosts like this (specific info hidden):

<VirtualHost *:80>
        ServerAdmin someone@gmail.com
        DocumentRoot /var/www/vhosts/default/
        ServerName {my-dedicated-ip}
        ErrorLog /var/www/vhosts/default/error.log
        CustomLog /var/www/vhosts/default/custom.log gk

<VirtualHost *:80>
    ServerAdmin some-email@valid-domain.com
    DocumentRoot /var/www/vhosts/something/httpdocs/
    ServerName valid-domain.com
    ServerAlias *.valid-domain.com
    ErrorLog "| /usr/sbin/rotatelogs -l /var/www/vhosts/something/logs/error.log.%Y.%m.%d 86400"
    CustomLog "| /usr/sbin/rotatelogs -l /var/www/vhosts/somthing/logs/custom.log.%Y.%m.%d 86400" gk

but unfortunately it doesn't work.

Thanks in advance.

EDIT: Oh, and i have a single dedicated ip.

I also just found this: How can I get Apache to not respond to an IP-only request? according to which, my setup should work. It should visit the first vhost it finds. But it doesn't. It goes to the second.

EDIT: apachectl -S output:

[root@me conf.d]# apachectl -S
VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:80                   is a NameVirtualHost
         default server domain.com (/etc/httpd/conf/httpd.conf:1015)
         port 80 namevhost {my-dedicated-ip} (/etc/httpd/conf/httpd.conf:1015)
         port 80 namevhost valid-domain.com (/etc/httpd/conf/httpd.conf:1023)
                 wild alias *.vaild-domain.com
Syntax OK

and yes the 2 vhosts are in the same file.

  • Are both vhosts defined in the same config file? What's the output of apachectl -S? – etagenklo Jun 30 '14 at 11:25

In Apache 2.2 you designate a default virtual host with the _default_ directive which should match for both the ip-address as well as all domain names that are not explicitly defined as ServerName or ServerAlias in the subsequent VirtualHost entries.

For instance:

<VirtualHost _default_:80>
        DocumentRoot /var/www/vhosts/default/
        ServerName default

<VirtualHost *:80>
    DocumentRoot /var/www/vhosts/something/httpdocs/
    ServerName valid-domain.com
  • Finally! It's working. At first it didn't but i remember to hit a Ctrl+F5 and the other page came up. Thank you very much. I have tried that with the default but not with ServerName default.. So that was it! Another question. You say specifically for Apache 2.2. If i update to 2.4 will i need to change something? – GktrGR Jun 30 '14 at 11:59
  • In apache 2.4 the first VirtualHost entry is the default and IIRC the _default_ isn't a valid directive anymore, in apache 2.2 the _default_ need not be the first virtual host entry. – HBruijn Jun 30 '14 at 13:07

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.