Using .htaccess to Redirect a Domain or Multiple Domains
When managing multiple domains pointing to the same webspace, designating a primary domain can become challenging. Without proper redirection, users might access your site through different domains, which can hurt SEO and create confusion. Using Apache’s Rewrite Rules in your .htaccess
file is an effective way to handle domain redirection and ensure users are directed to the correct URL.
This guide will show you how to use .htaccess to redirect multiple domains to a single primary domain, whether you prefer including or excluding the www
subdomain.
Example 1: Redirect to a Primary Domain with www
The following code snippet redirects all incoming traffic to a specified domain (e.g., www.newdomain.com
), regardless of which secondary domain was initially accessed.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !newdomain.com$ [NC]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [L,R=301]
Example 2: Redirect to a Primary Domain Without www
If you prefer to redirect all domains to a version without www
, use this snippet instead:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !www.newdomain.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301]
Explanation of the Code
Let’s break down the code to understand how these rewrite rules work:
RewriteEngine On
: Enables Apache’s mod_rewrite engine, allowing you to create rewrite rules.RewriteBase /
: Sets the base URL for the rewrite engine to the root directory of your webspace.RewriteCond %{HTTP_HOST} !newdomain.com$ [NC]
: This condition applies the rewrite rule only if theHTTP_HOST
(domain) does not matchnewdomain.com
. The!
negates the condition, meaning it triggers when the domain is notnewdomain.com
.[NC]
: Stands for "No Case", making the domain comparison case-insensitive.
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [L,R=301]
:^
: Anchors the rule to the beginning of the string.(.*)
: Captures any path after the domain name.http://www.newdomain.com/$1
: Redirects towww.newdomain.com
, appending the original path ($1
) after the new domain.[L,R=301]
:L
: Last rule—stops processing further rules if this one matches.R=301
: Sends a 301 Moved Permanently response, indicating that the redirection is permanent.
Implementation
To implement these rules, place the chosen code snippet in your .htaccess
file located at the root of your webspace. Ensure that the mod_rewrite
module is enabled on your server for these rules to function.
# Place this code in your .htaccess file
Use Cases
- Redirecting Multiple Domains to One Primary Domain: For example, if you own both
example1.com
andexample2.com
but want all traffic to point towww.primarydomain.com
, these rules ensure that visitors are always directed to the main site. - SEO Benefits: Consistent redirection prevents duplicate content issues, helping search engines index the correct domain.
- User Experience: Ensures a unified experience, so users don’t end up on different versions of your site.
Practical .htaccess
Configurations
Here are a few configurations for common scenarios:
Redirect All Traffic to a Primary Domain (With www)
Use this code snippet to redirect all incoming traffic to www.primarydomain.com
:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.primarydomain.com$ [NC]
RewriteRule ^(.*)$ http://www.primarydomain.com/$1 [L,R=301]
Redirect All Traffic to a Primary Domain (Without www)
If you prefer to drop the www
, use the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^primarydomain.com$ [NC]
RewriteRule ^(.*)$ http://primarydomain.com/$1 [L,R=301]
Redirect a Specific Domain to a Subdirectory
For cases where you want to redirect a domain (e.g., old-domain.com
) to a subdirectory on another domain (e.g., new-domain.com/subdir
):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old-domain.com$ [NC]
RewriteRule ^(.*)$ http://new-domain.com/subdir/$1 [L,R=301]
Protecting Your htaccess File
It’s good practice to restrict access to your .htaccess
file to prevent tampering:
<Files .htaccess>
Order Allow,Deny
Deny from all
</Files>
This directive blocks all external access to the .htaccess
file, enhancing security.
Further Notes
- Always test your redirection rules to ensure they work correctly without creating infinite loops.
- Backup your .htaccess file before making any changes. A single typo can make your site inaccessible.
- For more advanced redirection scenarios, consider consulting the Apache mod_rewrite documentation.
By implementing the above techniques, you can easily manage domain redirection using .htaccess
, ensure consistent user experience, and improve SEO by pointing all traffic to your desired primary domain.
Would you like to explore other redirection scenarios or need assistance with a specific use case?