Skip to content

Instantly share code, notes, and snippets.

@wielinde
Forked from furkanayhan/blog_controller.rb
Last active February 3, 2016 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wielinde/4b22df86bcd239be7991 to your computer and use it in GitHub Desktop.
Save wielinde/4b22df86bcd239be7991 to your computer and use it in GitHub Desktop.
You can mount your Tumblr Blog to your existing Rails app on /news. It is SEO optimized as it tells Google only to index yourdomain.com and not yourdomain.tumblr.com routes. Forked from: https://gist.github.com/petewarden/3950261
YourApp::Application.routes.draw do
get '/news(/*tumblr_path)' => "tumblr#proxy"
end
# Add this line to the <head> of your template template. :
<meta name="ROBOTS" content="NOINDEX, NOFOLLOW">
class TumblrController < ApplicationController
def proxy
source_url = 'https://yourdomain.tumblr.com'
path = params[:tumblr_path]
if path
source_url = File.join(source_url, request.path.gsub(/^\/news/, ''))
end
source_content_type = ''
source_body = open(source_url) do |f|
source_content_type = f.content_type # "text/html"
f.read
end
if source_content_type == 'text/html'
output_base = request.base_url + '/news'
# set canonical_url to yourdomain and tell google to not index yourdomain.tumblr.com :-)
output_body = source_body.gsub(/<meta name="ROBOTS" content="NOINDEX, NOFOLLOW">/, "<link rel=\"canonical\" href=\"#{request.url}\">".html_safe)
output_body = output_body.gsub(/\b(href|src|rel)="\/\//, '\1="https:\/\/')
output_body = output_body.gsub(/\b(href|src|rel)="\//, '\1="' + output_base + '/')
output_body = output_body.gsub(/\b(href|src|rel)="https:\/\/yourdomain\.tumblr\.com/, '\1="/news')
elsif source_content_type == 'text/xml'
output_body = source_body.gsub(/(<link>|<guid>)https:\/\/yourdomain\.tumblr\.com/, '\1https://yourdomain.com/news')
else
output_body = source_body
end
render :text => output_body.html_safe, :content_type => source_content_type
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment