Wordpress Inside a Ruby on Rails App

How to setup a Wordpress blog to work inside a subdirectory of any Ruby on Rails app

Paul Arterburn
4 min readAug 5, 2014

Wordpress & Rails don’t usually play nice together, but for maximum search engine optimization you want your blog as a subdirectory — not as a subdomain. Here’s the step-by-step guide of how we were able to get our Wordpress blog as a subdirectory of our Ruby on Rails app which is hosted on Heroku.

Do it for the SEO

I would still strongly urge folks to keep all content on a single subdomain. We recently were able to test this using a subdomain on Moz itself (when moving our beginner’s guide to SEO from guides.moz.com to the current URL http://moz.com/beginners-guide-to-seo). The results were astounding — rankings rose dramatically across the board for every keyword we tracked to the pages.

Rand Fishkin, Co-founder of Moz.com, explains it best above (full answer here). In short, it makes a big difference. Get your blog as a subdirectory as fast as possible, otherwise you’re not maximimzing pageviews for the content you’re putting out.

Setup Wordpress

Flywheel Management: No need to add a subdomain here. The password protection helps the search engines know to only visit the blog where you want it seen.

We have our Wordpress blog hosted on Flywheel (use the Flywheel discount code unreasonable for 20% off; disclaimer: my referral link). The support is incredible, and I highly recommend them. It’ll make the rest of this a breeze.

Email Flywheel and tell them you need to setup your blog like rovrblog.flywheelsites.com. They’ll need to change the Wordpress Address and Site Address (these fields are locked in Flywheel from users changing them).

Ask for Dusty (one of the Flywheel founders who worked with us) if you run into any issues.

Flyhweel will need to change the URLs inside Wordpress Admin to match the proxy address.

You can absolutely host your blog outside of Flywheel, but you may have a harder time following in the guide. If you run into hiccups, I’d recommend giving Flywheel a run before scrapping the idea.

Setup your Rails App

Add the rack-reverse-proxy gem to your gemfile:

gem 'rack-reverse-proxy', :require => 'rack/reverse_proxy'

Add the reverse_proxy code in your application.rb file so that /blog/ points to your blog on Flywheel. If you’re running into issues at this stage, you might check out the gem’s documentation for some troubleshooting tips.

# config/application.rbconfig.middleware.insert(0, Rack::ReverseProxy) do
reverse_proxy_options preserve_host: true
reverse_proxy /^\/blog(\/.*)$/, 'http://fill-me-in.flywheelsites.com$1', :username => 'flywheel', :password => 'fill-me-in', :timeout => 500
end

Note: Keeping the password enabled on Flywheel helps keep anyone from visiting the site from anything other than your domain. Nobody (and Google especially) needs to see the site at the flywheelsites URL — this prevents it.

Forward /blog to /blog/

Since the blog is technically hosted at /blog/, we need to forward the /blog requests there (notice the non-trailing slash).

Add to config/routes.rb:

get '/blog' => redirect("https://yourdomain.com/blog/")

Forward an existing subdomain (optional)

If you had already been using a domain like blog.yourdomain.com before, you’ll also want to setup a forward for the subdomain as well:

Add to config/routes.rb:

# Only necessary if you had blog.yourdomain.com setup before:get '(*path)', to: 'application#blog', constraints: { subdomain: 'blog' }

Add to app/controllers/application_controller.rb:

def blog
redirect_to "https://yourdomain.com/blog#{request.fullpath.gsub('/blog','')}", :status => :moved_permanently
end

If needed, here’s a guide for how to Implement Routing for Subdomains.

Turn on SSL (optional)

If your existing site has SSL turned on (which it should), you’ll want to do a few more things in order to turn on SSL throughout the blog. Google is now ranking sites higher for SSL, so you should definitely take this extra step even if you are not today.

The free Simple SSL in Flywheel should suffice if your site doesn’t need the absolute maximum compatibility (or you don’t already have an SSL certificate). Otherwise, you’ll need to enable the $10/mo SSL add-on. They’ll send you a .CSR file, but you’ll also need the .KEY file in order to update your certificate on Heroku — just shoot an email to help@getflywheel.com and they’ll send it over. The site I setup had their certificate through GoDaddy, so I followed this guide to re-key it.

  1. Re-key certificate on GoDaddy using the .CSR file that Flywheel sent you
  2. Download the new certificate & combine them (shown here)
  3. Run heroku certs:update combined.crt server.key in order to update your certificate on Heroku (server.key is the file that Flywheel sent you)

That updates your SSL for your site on Heroku. Flywheel will take care of updating it on their end.

You’ll want to install the two plugins on your Wordpress to force it into SSL mode:

  1. Force SSL URL Scheme
  2. Wordpress HTTPS

You’ll also want to take a look at this article in order to turn back on the Visual Editor inside Wordpress.

Thanks for reading! If you need any help in getting this setup, don’t hesitate to tweet me: @parterburn.

--

--

Paul Arterburn
Paul Arterburn

Written by Paul Arterburn

Director of Engineering for @Unreasonable, maker of http://Dabble.Me, co-founder of @Brandfolder