Skip to content

Instantly share code, notes, and snippets.

@lylepratt
Created April 23, 2012 02:05
Show Gist options
  • Save lylepratt/2468246 to your computer and use it in GitHub Desktop.
Save lylepratt/2468246 to your computer and use it in GitHub Desktop.
Put Your Tumblr Blog in a SubURL (subdirectory) instead of a Subdomain using a Python proxy. It's better to host your blog in a subdirectory rather than a subdomain because Google treats subdomains as totally separate sites which is bad for SEO.
def blog(request, url=""):
remote = "http://YOURTUMBLRSITE.tumblr.com"
local = ""http://www.example.com/blog"
conn = httplib2.Http()
#This is to support Tumblr's search.
if request.GET.get("q"):
urlencode = lambda s: urllib.urlencode({'x': s})[2:]
url = url+"/"+urlencode(request.GET.get("q"))
proxy_url = "%s/%s" % (remote, url)
resp, content = conn.request(proxy_url, request.method)
p = re.compile('(.+id="content".+)(href|src|rel|action)="\/(.+id="footer".+)', re.DOTALL)
content = re.sub(p, r'\1\2="%s/\3' % (local), content)
content = content.replace(remote, local)
return HttpResponse(content)
@lylepratt
Copy link
Author

To copy paste that, you would need a site running on Django. If you google around, someone might have already written something similar in PHP, which you could probably just save to a directory on your web server.

@matteagan
Copy link

matteagan commented May 4, 2012 via email

@shahafabileah
Copy link

Thanks for posting this! A few questions...

  1. This will result in the same content appearing under two URLs: under tumblr.com and under yourdomain.com/blog. Right? I've heard that Google dislikes duplicated content, so doesn't this counteract the SEO benefit somewhat?
  2. It looks like the code does search-and-replace to make sure all links in the content point to yourdomain.com/blog instead of tumblr. Is that right? Does it work in practice? Are there any links that are missed (e.g. generated by JS, added via Ajax, etc)? Are there URLs that get mistakenly converted (e.g. Images hosted on tumblr servers)?
  3. Is Tumblr cool with a setup like this? Does it jive with their policies?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment