ThisCalculator

Everyday

Set Up Multiple Domain Aliases on Apache and Nginx

Updated · Standard formula, shown below

Setting up multiple domain aliases lets you serve the same website content from several domain names, such as example.com, example.net, and example.org, without duplicating your files. This guide walks you through configuring both Apache and Nginx on a typical Linux server, covering the essential steps from DNS to virtual host configuration.

What You’ll Need

  • A Linux server with Apache or Nginx installed and running.
  • Root or sudo access to edit configuration files.
  • All domain names you want to use must have DNS A records pointing to your server’s IP address.
  • A text editor (nano, vim, etc.) and basic familiarity with the command line.

Step-by-Step

For Apache

  1. Locate your virtual host configuration. On most Debian/Ubuntu systems, these are in /etc/apache2/sites-available/. On RHEL/CentOS, they are in /etc/httpd/conf.d/. You will typically have a default configuration file for your main site.
  2. Open the configuration file for editing. For example: sudo nano /etc/apache2/sites-available/example.com.conf
  3. Add the alias domains to the ServerAlias directive. Inside the <VirtualHost> block, you should already have a ServerName directive. Add a ServerAlias line listing all additional domains, separated by spaces. For example:
    <VirtualHost *:80>
        ServerName example.com
        ServerAlias example.net example.org www.example.net www.example.org
        DocumentRoot /var/www/html
    </VirtualHost>
  4. Save the file and exit the editor.
  5. Test the configuration. Run sudo apachectl configtest or sudo apache2ctl -t. You should see Syntax OK.
  6. Enable the site if it’s not already enabled. Use sudo a2ensite example.com.conf if you created a new file. If you edited an existing enabled site, skip this step.
  7. Reload Apache. Run sudo systemctl reload apache2 (or sudo systemctl reload httpd on RHEL/CentOS).

For Nginx

  1. Locate your server block configuration. On most systems, these are in /etc/nginx/sites-available/ (Debian/Ubuntu) or /etc/nginx/conf.d/ (RHEL/CentOS).
  2. Open the configuration file for editing. For example: sudo nano /etc/nginx/sites-available/example.com
  3. Add the alias domains to the server_name directive. Inside the server block, modify the server_name line to include all domains, separated by spaces. For example:
    server {
        listen 80;
        server_name example.com example.net example.org www.example.net www.example.org;
        root /var/www/html;
    }
  4. Save the file and exit the editor.
  5. Test the configuration. Run sudo nginx -t. You should see syntax is ok and test is successful.
  6. Enable the site if needed. On Debian/Ubuntu, create a symlink: sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/. If you edited an existing enabled site, skip this step.
  7. Reload Nginx. Run sudo systemctl reload nginx.

Common Mistakes

  • Forgetting DNS records. If a domain isn’t pointed to your server, the alias won’t work. Verify with dig example.net that it resolves to your server IP.
  • Using commas or other separators. Both Apache and Nginx expect spaces between domain names, not commas or semicolons.
  • Not including the www versions. If you want www.example.net to work, you must list it explicitly. Wildcard like *.example.net is possible but catches all subdomains.
  • Editing the wrong configuration file. On Debian/Ubuntu, you must edit files in sites-available and then enable them, not directly edit sites-enabled (though it works, it’s not persistent across updates).
  • Forgetting to reload the web server. Changes only take effect after a reload or restart.

Tips

  • Use a canonical redirect. To avoid duplicate content issues, consider redirecting all alias domains to your primary domain. In Apache, you can add Redirect permanent / https://example.com/ in a separate virtual host for each alias. In Nginx, add a separate server block with return 301 for each alias.
  • Test each domain individually. Use curl -H "Host: example.net" http://localhost to verify the server responds correctly without waiting for DNS propagation.
  • Use a configuration management tool. If you manage many servers, consider using Ansible, Puppet, or Chef to automate alias setup.
  • Keep a backup. Before editing configuration files, copy them: sudo cp /etc/nginx/sites-available/example.com /etc/nginx/sites-available/example.com.bak.

Troubleshooting

  • Site not loading for an alias. Check DNS resolution first: dig example.net. If it resolves, check that the web server is listening on port 80/443: sudo netstat -tulpn | grep :80. Then verify the configuration is loaded: sudo nginx -T | grep server_name (Nginx) or sudo apache2ctl -S (Apache).
  • Getting a default site instead of your content. This usually means the alias isn’t matched. Ensure the ServerAlias or server_name is spelled correctly and that the virtual host is the first one that matches. In Apache, if you have multiple virtual hosts, the first one with a matching ServerName or ServerAlias wins. In Nginx, the first server block that matches the Host header is used.
  • SSL certificate errors. If you’re using HTTPS, you need a certificate that covers all aliases. Use a wildcard certificate (*.example.com) or a multi-domain (SAN) certificate. For Let’s Encrypt, use the -d flag for each domain: certbot --nginx -d example.com -d example.net.
  • Changes not taking effect. Run sudo systemctl reload apache2 or sudo systemctl reload nginx after every change. If you still see old behavior, check for syntax errors with configtest or nginx -t.

Frequently Asked Questions

Can I use wildcards in ServerAlias or server_name?

Yes. For example, ServerAlias *.example.net matches any subdomain of example.net. However, this also matches www.example.net and any other subdomain, which may not be intended.

Do I need separate virtual hosts for each alias?

No. You can list multiple aliases in a single virtual host (Apache) or server block (Nginx). This is the simplest approach. Separate blocks are only needed if you want different behavior (like redirects) for each alias.

Will this work with HTTPS?

Yes, but you need a certificate that covers all domains. A single certificate with multiple SANs (Subject Alternative Names) is required. Let’s Encrypt can generate such certificates.

How do I redirect all aliases to my primary domain?

In Apache, create a virtual host for each alias with Redirect permanent / https://example.com/. In Nginx, add a server block for each alias with return 301 https://example.com$request_uri;.

What if I want different content for each domain?

Then you should not use aliases. Instead, create separate virtual hosts or server blocks with different DocumentRoot or root directives.

Do I need to restart or reload the web server?

Reload is sufficient and does not drop active connections. Use sudo systemctl reload apache2 or sudo systemctl reload nginx. A restart is only needed if you change the main configuration file or listen ports.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.