redirect snippets

How to improve your PageRank with 301 permanent redirects when using Nginx

Tagged seo, www, 301, permanent, nginx, redirect, rewrite, module  Languages 

Mathew Innman of seomoz.org fame wrote about how Digg could increase their revenue by using a so called canonical URL for their whole site. This can be implemented by redirecting users that type in, for example, www.digg.com to digg.com. The reasoning being that instead of having backlinks pointing to two different domains (www and no-www), all backlinks should point to just one, which increases your search engine ranking.

if ($host ~* "www") {
      rewrite ^(.*)$ http://aktagon.com$1 permanent;
      break;
    }

Permanent redirects are also a good idea, if you move your content to a new domain--digg.com to dugg.com, for example...

The syntax for the Nginx rewrite module is documented here.

How to find out where a URL redirects with Ruby and Curl

Tagged curl, redirect, curb  Languages ruby
text = %x[curl -L -i "http://theredirectingurl.com"]
url = text.scan(/Location: ([^\n]+)/).flatten.last

With the Curb wrapper for Curl you could do this:

result = Curl::Easy.perform(link) do |curl| 
  curl.headers["User-Agent"] = "..."
  curl.verbose = true
  curl.follow_location = true
end
result.last_effective_url # Returns the final destination URL after x redirects...
result.url # Returns the original url which is redirecting...