I recently setup a staging site on our server's subdomain. Somehow Google managed to crawl the pages. Now we are ready to transfer the files to the main domain and delete the files on the subdomain. This is a big problem for our SEO which will make the pages on the root domain appear to be duplicates of the indexed pages on the subdomain. All paths on the subdomain is exactly the same with ones on the root domain, only the host part is different.

Here are some of the many different path structures of the site:

http://beta.mysite.com/ --> http://www.mysite.com
http://beta.mysite.com/index.php?class=xxx&func=xxxx --> http://wwww.mysite.com/index.php?class=xxx&func=xxxx
http://beta.mysite.com/class/xxx/func/xxx --> http://www.mysite.com/class/xxx/func/xxx
http://beta.mysite.com/seo-text-here-class-xxx-func-xxx --> http://www.mysite.com/seo-text-here-class-xxx-func-xxx
http://beta.mysite.com/xxx-xxx.html --> http://www.mysite.com/xxx-xxx.html

As you can see only the host part changed, everything else remains as it is.

QUESTION: Is it possible to redirect requests on the subdomain to do a SEO friendly redirect to the actual files on the root domain without having to explicitly write each rule for different path structures?

NOTE: The redirection should be transparent to users which means they should see that the page actually redirects to the main site.

1 Answer 1


Try putting these rules in your .htacccess file:

RewriteEngine on
Options +FollowSymlinks -MultiViews

# handles http redirect
RewriteCond %{SERVER_PORT} =80
RewriteCond %{HTTP_HOST} ^beta\.mysite\.com$ [NC] 
RewriteRule ^/?(.*)$ http://www.mysite.com/$1 [R=301,L,QSA,NE]

# handles https redirect
RewriteCond %{SERVER_PORT} =443
RewriteCond %{HTTP_HOST} ^beta\.mysite\.com$ [NC] 
RewriteRule ^/?(.*)$ https://www.mysite.com/$1 [R=301,L,QSA,NE]

R=301 will redirect with https status 301

L will make last rule

NE is for no escaping query string

QSA will append your existing query parameters

$1 is your REQUEST_URI

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.