Rob Morris
Rob Morris

Reputation: 533

Redirecting a web folder directory to another htaccess

Sorry this has no doubt been asked multiple times before, I just want clarification that the following code will redirect any url on olddomain.com to the newdomain.com homepage not the equivalent url:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.olddomain\.com$
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^olddomain\.com$
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

Also if I wanted any subdomain on olddomain.com eg.subdomain.olddomain.com to go to the homepage of newdomain.com what would I have to do? Can I use a universal selector or would I have to write a condition for each subdomain like so:

RewriteCond %{HTTP_HOST} ^subdomain.olddomain.com$ 
RewriteRule ^(.*)$ http://subdomain.newdomain.com/$1 [R=301,L] 
RewriteCond %{HTTP_HOST} ^www.subdomain.olddomain.com$ 
RewriteRule ^(.*)$ http://subdomain.newdomain.com/$1 [R=301,L]

Upvotes: 1

Views: 54

Answers (1)

anubhava
anubhava

Reputation: 785581

Both of attempts are not correct as first will redirect:

http://olddomain.com/foobar to http://newdomain.com/foobar

not to the homepage of newdomain. Same is the problem with 2nd rule.

You can use this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On

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

RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.olddomain\.com$ [NC]
RewriteRule ^ http://subdomain.newdomain.com/ [R=301,L]

Upvotes: 1

Related Questions