Redirect a Domain Alias to Primary Domain in Apache using mod_rewrite

This is perhaps one of the most simplest mod_rerwite directives that achieve something incredibly complex and useful. Say for example you have as your primary website domain. Then a few years later, you want to create an additional easy to remember domain that your visitors can use to get to For example, Or lets say in the event that you decide to rename your website and use a new domain (for example to replace your current one. You want to make sure your visitors can still access content from your old domain, while being redirected to the new domain.


  • Apache 1.3 or later
  • The mod_rewrite module installed and enabled in your Apache configuration.
  • These Instructions assume the usage of FreeBSD and Apache 2.2, but may be applied (with some modification) to Linux, OS X, and any compatible platform. Please refer with your platform's documentation for correct paths to the Apache configuration file(s)

Getting Started

The first step is to add a name alias to Apache so it known to answer requests for this new domain. Open up your /usr/local/etc/apache22/httpd.conf configuration file and add the following directive under your default website or virtual host:


Notice that we must include an entry for the domain with and without the all so popular "www" host.

You could stop here and Apache will answer requests for your new domain (assuming DNS is correctly setup) just as if they were coming from your primary domain. These two links would bring you to the same page (links are not really functional).

However, look at your address bar. Although both pages look the same when displayed, they have different web addresses (URI's).

The Rest of the Puzzle

In most cases it's not desirable to have two or more different URI's to access the same content. It can confuse visitors, and it's not search engine friendly. Sometimes it might even hurt your ranking. If your moving domains, this alone would be of no use to your visitors since they would not be redirected to the new domain..

This is were the mod_rewrite directives come into play. The URL rewriting feature in Apache takes the Alias directive a step further. It "re-writes" the URI on the visitors web browser so that it displays the correct domain name.

Add the following to your /usr/local/etc/apache22/httpd.conf to turn on the URL rewriting engine:

RewriteEngine on

Next add the following underneath the directive shown above. This is a rule that looks to see if any domain other than your primary domain was used to get to this website.

RewriteCond %{HTTP_HOST} !^$

If this rule is matched, the next rule get's processed.

Add the following directly underneath the rule above. This is where the magic happens, so much gets done in such a short line of code. This rule takes the URI:, transforms it into, and issues a search engine friendly 301 redirect back to your browser. Your browser then loads the page using the correct domain.

RewriteRule (.*)$1 [R=301,L]

Or, if you were moving to, you'd use the following:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^$
RewriteRule (.*)$1 [R=301,L]

This would redirect all your visitors to the new domain without breaking existing links to your old domain.

How to Use it for Your Website

It may look cryptic, but it's simple once you get past the code. Understanding the code concepts behind it is not important unless you want to one day write your own rules. For now, just copy and paste the directives below into your /usr/local/etc/apache22/httpd.conf file. Replace the words "REAL-DOMAIN.HERE" with the domain that you want to force your visitors to use. Then replace "OTHER-DOMAIN.HERE" with your alias(es). You may add as many "ServerAlias" directives as you need, just remember to make ones for the "www" host if needed.

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.REAL-DOMAIN.HERE$
RewriteRule (.*) http://www.REAL-DOMAIN.HERE$1 [R=301,L]

Make sure you reload Apache with the following command to apply any configuration changes.

apachectl restart