How to redirect subdomains using Htaccess 301 redirects

in Tutorials & Samples/Web Development

Whenever you are moving a site, it is best practice to use Htaccess 30 redirects. This is the standard practice for SEO purposes and it is also the method recommended by Google. I’ve been using this technique for a several years now and it works perfectly. For example if I wanted to move the contents of my current site to newsite.com, I could use the following syntax in my .htaccess file:

Redirect 301 / http://newsite.com/

However, recently, I had to move a subdomain and ran into problems with the 301 redirect.

Here’s the problem: I needed to redirect all URLs on a subdomain to completely different URL on a different subdomain. So:

http://example.oldsite.com/this-is-my-page

Needed to 301 to:

http://example.newsite.com/this-is-really-my-page

I tried several up a simple 301 redirects in .htaccess but they all didn’t work. Some of my tests were:

Redirect 301 http://example.oldsite.com/this-is-my-page http://example.newsite.com/this-is-really-my-page

and

Redirect 301 example/this-is-my-page http://example.newsite.com/this-is-really-my-page

None of these techniques worked until I used the solution below.

redirecting subdomains using Htaccess 301 redirects

It turns out that 301 redirects recognize subdomains as separate websites. So all I had to do was create a new htaccess file inside the main folder of the subdomain and point it to the new site. So, if I had a subdomin on my old site called subdomain and it was located at oldsite.com/subdomain, I simply had to create a new htaccess file inside oldsite.com/subdomain and have it point to the new site and it works like magic.

Tags:

Mifty Yusuf is a Montreal-based software developer who enjoys playing with new web technologies as well as comic books and illustrations. He beleives that, no matter what the question is, the answer is always Batman!

3 Comments

  1. Wondering if you can help me.

    I have this site which is a sub-domain I used to develop a site, now I am done developing the site on my sub-domain and want to begin the process of using 301 redirects to switch from my development site to the new redesigned site.

    Do i simply follow this pattern,

    Redirect 301 /old-page.com http://www.example.com/new-page?

    • Hi Ante,
      You could do it this way, but the better way to do it would be to simply override the old content tiwht the new content. (in other words, move everything over from the development folder to the production folder). The reason why this is a better option is that you do not want to loose your page ranks for SEO purposes. When you do a 301 redirect, your pageranks are not automatically transferred. (sometimes it takes several months for Google to transfer over the PR values). Another reason is that the new pages will loose all social bookmakrs. So, all facebook likes and Google pluses that you had on the old page will not get transferred over with the 301 redirect.

Leave a Reply

Your email address will not be published.

*

Latest from Tutorials & Samples

Go to Top