How to Redirect a Subdomain to a Root Domain (e.g. staging.mysite.com/stuff to mysite.com/stuff)

I had a Google Search Console issue today where it was seeing a bunch of staging urls that no longer exist. I don’t know how they got there, but here we are. 🙂 To resolve this issue, I had to:

  1. Add the staging url to the server as an addon domain.
  2. Add the following to .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^mydomain.com$ [NC]
RewriteRule ^(.*)$ https://mydomain.com/$1 [R=301,L]
  1. Update the dns “A” record for “staging” so it points to my root domain server.

That’s it!

Posted in

Toby Cryns

Toby Cryns is a freelance CTO, expert WordPress consultant, and teacher.

He offers free advice to improve your freelance biz.

He also publishes small droppings every now and then to twitter.com/tobycryns and twitter.com/themightymo

Follow Toby's contributions on Github and WP.org.