We help IT Professionals succeed at work.
Get Started

mod_rewrite for redirecting

241 Views
Last Modified: 2013-11-30
I would like to create a mod_rewrite rule that will do the following:

Just say I have the following domain names:

domain1.com
domain2.com
domain3.com
domain4.com

domain1.com is our main domain and we want all the other domains to redirect to domain1.com. I have managed to do this using the following .htaccess file:


Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on

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

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

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


Two Questions

1. Is it possible to create one rule to do this because I have multiple domains that will take a long time to write.

2. Is it possible to have it so that if somone uses say company.domain2.com it is redirected to the main domain but with the subdomain they specified eg company.domain1.com
Comment
Watch Question
Top Expert 2005
Commented:
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE