Posts Tagged with “htaccess”

Bulletproof domain redirection for CPanel Add-On domains

Recently I helped my friend Beth Heavrin of Smart Green Tips to migrate her blog to a new domain name. Along the way I realized that the simple way of domain redirection doesn’t work as well for websites that are add-on domains in CPanel environments. This article shows you how to create bulletproof redirects that will work, even for add-on websites.

The Problem

CPanel site structure

In CPanel environments, you start off with a primary domain that resides in the public_html directory. When you add new site to your CPanel hosting, they are added as add-on domains whose root folder are subdirectories of public_html.

You end up with a directory structure in your web hosting account like the image on the right.

Simple .htaccess Redirects Don’t Work

For our example, let’s assume that the primary domain is and the new domain we want to redirect it to is To redirect to, you may have used this code in the .htaccess file for

# Redirect 301 /

Or, you may have used this code:

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

With the code above, will get redirected to But unfortunately visitors to will be wrongly redirected to Same for other add-on domains within the public_html directory.

This is because is a subdirectory of public_html. The 2 code snippets above redirect everything, including add-on domain subdirectories to Confused? Never mind! Just use the solution below.

The Solution

The solution is to check the hostname (URL) that the visitor is trying to visit before redirecting them. So, let’s update our .htaccess file to the following:

# Redirect to
RewriteCond %{HTTP_HOST} ^$
RewriteRule ^(.*)$$1 [R=301,L]

#Redirect to
RewriteCond %{HTTP_HOST} ^$
RewriteRule ^(.*)$$1 [R=301,L]

The first block of rules only redirects visitors to and the second block only redirects visitors to Visitors to or will not be redirected. Solved!