xRobot
xRobot

Reputation: 26565

How to redirect from domain to subdomain?

My site is www.mysite.com and I need to redirect any request to us.mysite.com.

So:

 www.mysite.com ----> us.mysite.com
 www.mysite.com/hello.php ----> us.mysite.com/hello.php
 // etc

I tryed this but doesn't work:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com$
RewriteRule (.*) http://us.mysite.com$1 [R=301]

Upvotes: 0

Views: 106

Answers (2)

showdev
showdev

Reputation: 29188

It looks like your RewriteCond is only matching domains that start and end with mysite.com. This does not include www.mysite.com.

The following will 301 redirect anything NOT at us.mysite.com to us.mysite.com:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^us.mysite.com$
RewriteRule ^(.*)$ http://us.mysite.com/$1 [R=301]

Upvotes: 2

yrk
yrk

Reputation: 165

There are several different solutions. The best one, both from SEO and User perspective, is the one-to-one 301 redirect. It preserves your link juice and at the same time redirects the client to the exact location on the new website.

If you have mod_alias enabled, I would suggest a simple

RedirectMatch 301 ^(.*)$ / http://new.domain.com/$1 The result instruction can be accomplished with

RewriteEngine On RewriteRule (.*) http://new.domain.com/$1 [R=301,L] The second one is the best choice if you need to chain multiple conditions and filters. For example, if you need to redirect only certain hosts or clients depending on User Agent header.

From here.

Upvotes: 0

Related Questions