3

My VirtualHost config is below:

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

I want to add a subdomain as such: new.example.com

And I want to add a wildcard to catch all other subdomains (*.example.com) and route them to www.example.com.

How should I add this subdomain? Will it involve an A record?

For the wildcard, if I change my ServerAlias to...

ServerAlias example.com *.example.com

... then will it interfere with my subdomain (new.example.com)?

EDIT: I have several domains on a single host (via virtual hosts). This is in the same folder as 000-default.conf, but it's named example.conf. (I've actually deleted 000-default.conf, and used conf files named after the domains they point to.)

  • Before I can answer this, I have to know what configuration file you got this from? Was it httpd.conf? 000-default.conf? This will help a lot in how your question is answered. – closetnoc Jul 21 '15 at 2:05
  • @closetnoc I have several domains on a single host (via virtual hosts). This is in the same folder as 000-default.conf, but it's named abcd.conf. (I've actually deleted 000-default.conf, and used conf files named after the domains they point to.) – thanks_in_advance Jul 21 '15 at 3:02
  • I decided to write an answer anyway... but it sounds like you have the newer standard configuration. This is good! Please ignore my caution. It is also likely that your 000-default.conf file is a catch-all site and you will not have to mess with this at all. I have seen sites define in this file which I recommend against. I recommend catch-all sites!! – closetnoc Jul 21 '15 at 3:06
  • Let me know how it goes!! You might have to fiddle with somethings here and there, but I rather suspect you know enough of what you are doing to get through all right. – closetnoc Jul 21 '15 at 3:13
1

Okay. I am going to suggest a little bit of work that will save you some effort down the road.

I have to make some assumptions in this answer in order to help.

You will have three distinct tasks: one, editing your configuration files; two, define your sub-domain in DNS; three, restart Apache.

I will also suggest making the site without www and creating the site alias with www. It is just a bit more proper.

If you have httpd.conf, all goes in one file, however, I warn you that the Apache configuration has been updated a long time ago except for a few Apache installs such as RedHat and one or two others. Each Linux has it's own package of course. I also warn you that it has been so long since I have dealt with httpd.conf that you will want to research this.

If you have 000-default.conf, I suggest doing yourself a favor and not use this file for any actual working site and preserve it for your catch-all site. I suggest a catch-all site to help with issues later on, such as IP address only accesses and accesses for domains/sites that do not exist on your server.

I am making the assumption that you have your site defined in 000-default.conf. If not, then ignore my recommendations for 000-default.conf and simply modify your existing configuration file and create one for your sub-domain.

If you have 000-default.conf, you will need to create a conf file for each site. Here is an example of what I am thinking.

Create the catch-all site (/etc/apache2/sites-available/000-default.conf):

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Create your parent site (/etc/apache2/sites-available/example.com.conf):

<VirtualHost *:80>
        ServerName example.com
        ServerAlias www.example.com
        DocumentRoot /home/example.com/www
        ErrorLog /home/example.com/log/example.com_error_log
        CustomLog /home/example.com/log/example.com_access_log combined
        DirectoryIndex index.php
<Directory /home/example.com/www>
        Options -Indexes +IncludesNOEXEC +FollowSymLinks +ExecCGI
        Allow from all
        AllowOverride All
</Directory>
        RewriteEngine on
</VirtualHost>

Create your sub-domain (/etc/apache2/sites-available/my-sub-domain.example.com.conf):

<VirtualHost *:80>
        ServerName my-sub-domain.example.com
        ServerAlias www.my-sub-domain.example.com
        DocumentRoot /home/my-sub-domain.example.com/www
        ErrorLog /home/my-sub-domain.example.com/log/my-sub-domain.example.com_error_log
        CustomLog /home/my-sub-domain.example.com/log/my-sub-domain.example.com_access_log combined
        DirectoryIndex index.php
<Directory /home/my-sub-domain.example.com/www>
        Options -Indexes +IncludesNOEXEC +FollowSymLinks +ExecCGI
        Allow from all
        AllowOverride All
</Directory>
        RewriteEngine on
</VirtualHost>

Of course you do not actually need to move your current website. You would just change your DocumentRoot directive for your parent site. As well, I included some other configuration stuff. You will want to research what you actually need. Pay particular attention to security. You do not want to simply cut and paste this example configuration "as is" though it should work short of the domain name of course.

The next thing you will need to do is define your DNS record where your sites SOA (statement of authority) is defined. Often, this is your registrar, but it does not have to be.

Your DNS record would be something like your example.com and www.example.com entries which should be similar to this:

example.com    A    10.0.3.201
www.example.com    CNAME    example.com

my-sub-domain.example.com    A    10.0.3.201
www.my-sub-domain.example.com    CNAME    my-sub-domain.example.com

It will take some time for DNS records to propagate throughout the Internet. Expect anywhere from 24-72 hours though some DNS servers will pick-up your changes quicker than that.

The last thing you will need to do is restart Apache to make sure that your sites are recognized. Changes within configuration files are cached when Apache restarts.

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.