Thursday, January 13, 2011

VirtualHosts + DNS Wildcard Subdomains

I'm a complete linux and server noob but I've started a project in an attempt to learn. What I'm trying to do right now is get wildcard subdomains to work.

In my vhost (sites-available) file I have:

<VirtualHost *.80>
ServerName domain.com
ServerAlias www.domain.com domain.com *.domain.com

etc..

In my domain registrar DNS settings I have an A record for @, www and * pointing to my server IP. Now to my knowledge this should be working, however when I go in my browser and browse to *.domain.com it comes up with the default apache text "It works!", when I go to straight to domain.com I can see my web page (which I want to be displaying for *.domain.com too).

Any help would be greatly appreciated! I'm probably missing something blindingly obvious but I'm a noob :)

  • Try this

    NameVirtualHost *:80 # enable name virtualhosting
    <VirtualHost *:80>  # :80, not .80
       ServerName domain.com
       ServerAlias *.domain.com # www.domain.com matches *.domain.com
       ...
    </VirtualHost>
    
    Jason Tan : Apache comments are started with '#' characters not "//". So don't put the "//"s in your config. Replace them with '#'s.
    Dave Cheney : Good point, I'll update my answer
  • i never know about writing ServerAlias option like this

    ServerAlias www.domain.com domain.com *.domain.com
    

    if you did well for dns config, try to create vhost config for the real domain (default domain), then create another vhost config for the wildcard after all real domain vhost config.

    <VirtualHost *:80>
    DocumentRoot "/var/www/html/abc"
    ServerName www.abc.com
    ServerAlias www.abc.com
    </VirtualHost>
    
    <VirtualHost *:80>
    DocumentRoot "/var/www/html/abc"
    ServerAlias *.abc.com
    </VirtualHost>
    
    Maxwell : as long as you put NameVirtualHost *:80 first i think
    From Gogonez
  • Nevermind, I forgot to restart apache.

    From zuk1

0 comments:

Post a Comment