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.)

MrWhite
  • 43,224
  • 4
  • 50
  • 90

1 Answers1

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.

MrWhite
  • 43,224
  • 4
  • 50
  • 90
closetnoc
  • 32,902
  • 4
  • 46
  • 69