How to Set up a DNS Alias With Apache

Serving multiple domains from an Apache web server

What to Know

  • Edit the apache.conf (version 1) or vhosts.conf (version 2) file, then find vhosts (virtual hosts) section. Edit as per below instructions.
  • After completing your edits, reboot the web server, then test the new configuration.

Set up DNS aliases with the Apache web server to point different hostnames to different directories on your web server.

Setting up DNS Aliases​

You can create one or more directories for content within your web server's file structure, but you can't put them in any location on your machine. For example, most Apache server web files are located in the htdocs folder. Create a sub-folder there to host your domain files—perhaps within a www folder to segregate the content from other uses. It's a good idea to put an index.html file in the directory so you can test later.

  • In version 1 of Apache, edit the apache.conf file and find the vhosts (virtual hosts) section.
  • In version 2 of Apache, edit the vhosts.conf file. These configuration files are usually located in a configuration directory on your web server, not in the htdocs area.

In either version, edit the vhosts section to add a new virtual host.

Setting DNS aliases on the web server doesn't change DNS settings. You must still configure your DNS to map an IP address to specific hostnames.

Structure of a Virtual Host Entry

A typical use case involves running different hostnames that resolve to the same Internet Protocol address, and each hostname must respond differently. For example, may reference a company's intranet site while may reference its public-facing web page. To configure these examples, edit the virtual-host file as follows:

# Ensure that Apache listens on port 80
Listen 80

DocumentRoot "/www/intranet"

# Other directives here
DocumentRoot "/www/public"

# Other directives here

For additional use cases for example virtual-host files, check out Apache's documentation.

Reboot the Web Server

After completing your edits, reboot the web server, then test the new configuration.

Was this page helpful?