.htaccess Redirects: www, non-www, and alternate domains

A white, rounded square logo with an abstract S-shaped design on a gradient blue and purple background.

Why Redirect?

For SEO purposes and marketing purposes, you often want to have all your domains redirected into a primary domain. This helps with telling the search engines and your visitors your primary web site address. Your client may have 2, 5, 10 or 20 domains and they all should point to the primary www version, so how do you do all the redirects?”¬† Easy.

When you are doing .htaccess rewrites for alternate domains, there is a difference between redirecting the non-www primary domain to its www version, and redirecting both the www and non-www of alternate domains to the primary www version.

Steps to Redirect

A single redirect of non-www code to the www version is listed below. If you are redirecting an alternate domain, you need to actually redirect both the www and non-www version. You can use an OR statement to redirect both the non-www alternate domain and www-alternate domain to the primary www version.

Wow that is a mouthful, so let’s use some examples:

Ex: To redirect a domain.com into www.domain.com.

This is if a client only has one domain and you want to make sure that the www version is the primary domain.

RewriteCond %{HTTP_HOST} ^domain.com

RewriteRule ^(.*)$ http://www. domain.com/$1 [L,R=301]

Ex: ”¬†alternatedomain.com and www.alternatedomain redirected to www.domain.com.

Use this code to redirect an alternate domain (both non-www and www) version to the primary domain. You can use this code multiple times, for as many domains as needed (see attached). As you can see, you can use the [OR] clause to use both the www and non-www in one rewrite rule, basically two conditions and one rule. This will reduce code in your .htaccess file.

RewriteCond %{HTTP_HOST} ^www. domain.biz [OR]

RewriteCond %{HTTP_HOST} ^ domain.biz

RewriteRule ^(.*)$ http://www. domain.com/$1 [L,R=301]

Share This

Join the Sales and Marketing News, receive our last insights, tips and best practices.

Our 7 Guarantees

Keeping 2,000+ Clients Happy Since 2001.

You Will Love Your Design We design to please you and your clients
Same-Day Support 24-hour turnaround edits during business hours
Free Education We provide knowledge to help you expand
No Hidden Charges We quote flat-rate projects
Own Your Site No strings attached
We Create Results SEO, PPC, content + design = clients
We Make Life Easier One agency for web, branding and marketing