How to write .htaccess for multiple domains?

by ervin.williamson , in category: SEO , 6 months ago

How to write .htaccess for multiple domains?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

1 answer

by genevieve_boehm , 6 months ago

@ervin.williamson 

To write an .htaccess file that manages multiple domains, you can use the RewriteCond and RewriteRule directives. Here's an example of how you can set it up:

  1. Open a text editor and create a new file named ".htaccess".
  2. Add the following code to your .htaccess file:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
RewriteEngine On
RewriteBase /

# Domain 1
RewriteCond %{HTTP_HOST} ^(www.)?domain1.com$ [NC]
RewriteRule ^(.*)$ http://www.domain1.com/$1 [L,R=301]

# Domain 2
RewriteCond %{HTTP_HOST} ^(www.)?domain2.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [L,R=301]


  1. Replace "domain1.com" with the first domain you want to redirect, and "domain2.com" with the second domain.
  2. Save the .htaccess file and upload it to the root directory of your website.


This example assumes that you want to redirect both "www.domain1.com" and "domain1.com" to "www.domain1.com", and similarly for "www.domain2.com" and "domain2.com". You can modify the code as needed to fit your specific requirements.