Redirect 301

I was looking at Google Analytics today and found that I was getting a bunch of traffic on Use Delicious with Chrome as well as some programming/technical articles, but on the wiki.futuretoby.com domain rather than on the tobyho.com domain. 

For those of you who don't know(probably all of you, because...why would you?), this "wiki/blog" runs the on 2 separate domains, but with a different facade over each one, if you will. One is for my family/personal stuff, for which most of the content it is intended for has access restrictions. The other is the tobyho.com domain, which I created more recently and is mainly for my public content. But all of the content can be accessed in exactly the same way on either domain.

So, basically, the former domain is still more visible on the Google search engine than the latter, and so people are coming to my articles on that one, rather than where I want them to go: tobyho.com. 

How to fix it? Well, it turns out that for things like this, a 301 redirect(as supposed to a 302) is the way to go. It will tell the search engine that the content has moved permantly, and so prompt it to update its index. The code I ended up putting together was pretty simple:

    tag_names = @page.tag_names
    if request.host != 'tobyho.com' and
        @page.authors.include?(toby) and 
        not tag_names.include?('family') and 
        (tag_names.include?('programming') or 
         tag_names.include?('tech') or 
         tag_names.include?('gadgets')) then
        headers["Status"] = "301 Moved Permanently"
        redirect_to("http://tobyho.com/#{@page.title_for_link}", :status => 301)
        return
    end

So, basically, if a visitor comes to an article from the other domain, I want to redirect to the tobyho.com domain if 1) I am the author, 2) the article is tagged with one of programming, tech, and gadgets and 3) the article is not tagged with family. The line:

headers["Status"] = "301 Moved Permanently"  

Is not needed if you're on a newer version of rails, for which setting :status => 301 on the next line would suffice. I embarrassed to say that I am still on 1.2.x.

blog comments powered by Disqus