djmzfKnm
djmzfKnm

Reputation: 27195

.htaccess - redirect domain name

I have a domain name www.domainA.com I want to redirect it to domainB in following manner.

www.domainA.com -> www.domainB.com
www.domainA.com/anything -> www.domainB.com/rebrand

How I can do this in htaccess, I have done following code but it redirecting to /rebrand/ only.

RewriteCond %{REQUEST_URI} ^\/
RewriteRule ^\/$ http://www.domainB.com/ [L,R=301]

RewriteCond %{HTTP_HOST} ^domainA\.com$ [NC]
RewriteRule ^(.*)$ http://www.domainB.com/rebrand/ [L,R=301]

Upvotes: 1

Views: 1063

Answers (2)

Rohit Reddy
Rohit Reddy

Reputation: 11

Redirecting through htaccess is tricky sometimes, there are many ways of implementing this but there is one simple way which worked for me

Options +FollowSymLinks 
RewriteEngine on 
RewriteRule (.*) [newdomain.com...] [R=301,L]

You can get more info from webmaster world forum

Upvotes: 0

Jon Lin
Jon Lin

Reputation: 143936

URIs that go through rules in an htaccess file has the leading slash stripped off, so you can't match against it. For the second rule, it's matching the / request because the first rule isn't being applied and your regex matches anything or nothing, you can fix that by changing the * to +:

RewriteCond %{HTTP_HOST} ^domainA\.com$ [NC]
RewriteRule ^/?$ http://www.domainB.com/ [L,R=301]

RewriteCond %{HTTP_HOST} ^domainA\.com$ [NC]
RewriteRule ^(.+)$ http://www.domainB.com/rebrand/ [L,R=301]

Upvotes: 2

Related Questions