Setting Up * Wildcard Domains On WHM/Cpanel

Are you looking to set up a wildcard domain like x.example.com and  y.example.com and have every other iteration land right at the same home directory in your web hosting account?  Or let me be more specific, are you trying to set up a new sub domain that will appear no matter what you put in.  For instance you have set up the whatever.example.com and now you want to always have any new “text” in front of the domain but have them all go to a single directory like test.example.com?  And finally do you use WHM/Cpanel?

If you have landed on my blog to figure this mystery out, I am going to reference some other discussion boards and blogs that give you specific instructions, but I will take you step by step what I did to set this up for myself and clarify some of the smaller items.

As far as WHM Cpanel is concerned, I believe you need to manage WHM directly.  So if you are using Cpanel, you are not going to be able to do this without the hosting company or administrator getting involved.  And then you have to use either a unix command line or ftp to make the final changes typically as root access.  That’s because there will be changes necessary to the apache configuration and the Cpanel user does not have this level of access.  While I say that, there still may be some way of using .htaccess to do this as a hack, because .htaccess is an extension of apache httpd.conf.  But, it would be a total hack, and I don’t believe it can be done because the wildcard implementation requires a Virtual Host entry change that you can’t do yourself.

Here are the steps:

1. Configuring Apache for the wildcard

This is the most difficult part of setting up a wildcard implementation on WHM/Panel.  It took me a few attempts to get this right and figure out what appears to be some confusing and not so helpful information.  Start by getting to the apache configuration and reviewing the current httpd.conf file.  It will be located at /usr/local/apache/conf/ directory.  I use the Linux command line or shell to move around, but you could ftp in as root and move over to this directory and view the httpd.conf file.

You may or may need to set up the basic domain name as full Cpanel account first.  This will create the directory and account you want the visitors to see as your root directory, plus WHM Cpanel configures things nicely. So if your domain name was example.com you would create a new account for this domain with a new user.  This will at least get you ready and get you a directory with files.

This instructional link gave me some insight into what you can and can not do in WHM/Cpanel.

Specifically he mentioned that you can not edit the httpd.conf file directly.  If you do, and rebuild the apache config file, you WILL LOSE your changes.  Below is from the apache documentation on setting up virtual hosts.  Notice this line:

<VirtualHost *:80>
    # This first-listed virtual host is also the default for *:80
    ServerName example.com
    ServerAlias *.example.com 
    DocumentRoot /www/domain
</VirtualHost>

# DO NOT EDIT. AUTOMATICALLY GENERATED.  IF YOU NEED TO MAKE A CHANGE PLEASE USE THE INCLUDE FILES.

I of course ignored the warning on writing to apache httpd.conf directly and did 
write it to test things out. So, if you are looking to add an additional alias 
like a *.example.com you can test things in the original file, but be aware you 
will lose your changes.  

There are 2 situations I ran into for wildcard domain setups. The first is setting 
up a wildcard for a domain that you have already configured.  For the situation where 
the wildcard is for an existing domain you have to write to 

/usr/local/apache/conf/userdata/std/2/userexample/example.com/*.conf

You can add 

ServerAlias *.example.com 

into a file.  They recommended calling the file extra.conf, but basically all files in this 
../user/example/example.com/ directory will get included when apache is restarted.

For the user name or "userexample" that becomes the "Account" user name.  example.com 
becomes the directory.  Yes, you have to create this domain tree and directories.  
Use mkdir to do this.  I believe only ../userdata/ existed. I had to create the rest 
of the directories manually.

The second situation is where you want to add a completely new wildcard domain and point it to
a specific directory.

Notice these lines at the bottom of the httpd.conf:

Include “/usr/local/apache/conf/includes/post_virtualhost_global.conf”
Include “/usr/local/apache/conf/includes/post_virtualhost_2.conf”

You can write lines in these files like (match the IP address of the hosting account you are point to):

<VirtualHost xx.yy.zz.aa:80>
    ServerName www.example.com
    ServerAlias example.com 
    DocumentRoot /www/domain
</VirtualHost>

Either way, you have to think a little to do this type of wildcard implementation, especially
on WHM/Cpanel.  Restart apache on Whm once you have pointed the proper A record on DNS.

2. The DNS entry
So let's talk about the DSN record set up.  Let's say you use Godaddy for your DNS.  I 
don't, but that's not important. You go under the A record under DNS and make sure there
is a wildcard or "*" placed as the domain name, instead of an example.com or www.example.com.  
In fact you can just remove thee www.example.com if you are going to use a star.  If it 
is already set up, you are good.  If you need to set it up, just drop in a star instead
of the domain name.

Restart apache under WHM and good luck.

 

 

Follow Dan Gudema on Google+!

Leave a Reply