IIRF v2.0 - Help with Domain's and Redirects

1.3k Views Asked by At

I have a primary domain - let's call it xyz.com. I also have several other secondary domains such as abc.com def.com, ghi.com, etc.. These domain all have the same content.

I am trying to do a URL redirect in IIRF that will take any of the secondary domains, and replace it with my primary xyz domain.

This is the closest I have gotten.

RewriteCond  %{HTTP_HOST}  ^(?!(.*)\.xyz\.com)$
RedirectRule ^/(.*)$        http://*1.xyz.com/$1   

Problem #1: with this if I navigate to 123.abc.com, I am brought to .xyz.com (I am losing my sub-domain, I thought I could retrieve that with '*1').

Problem #2: even when I go to www.xyz.com, I am redirected to .xyz.com this rule should obviously ignore any xyz.com domain

2

There are 2 best solutions below

0
On BEST ANSWER

I took me a week, but I got it.

##### handles 3-part domains like "xxx.yyy.com"
    RewriteCond %{HTTP_HOST} ^(.*)\.(?!xyz).*\.com$        
    RedirectRule ^/(.*)$ http://*1.xyz.com/$1 [R=301]

and

##### handles 2-part domains like "yyy.com"      
    RewriteCond %{HTTP_HOST} ^(?!xyz).*\.com$        
    RedirectRule ^/(.*)$ http://xyz.com/$1 [R=301]
5
On

this should work

#not in main.com
RewriteCond %{HTTP_HOST} !.*main.com [NC]
#get the subdomain as a backreference to use in the RewriteRule
RewriteCond %{HTTP_HOST} (.*)[a-z0-9]+\.[a-z]+$ [NC]
#Rewrite the new URL
RewriteRule ^(.*)$ http://%1main.com/$1 [NC,QSA,L]

I'm not the most uber regex guru though, so there may be some border cases which provide unexpected results