add wordpress blog

How to add a WordPress blog to a Ruby on Rails app?

While Ruby on Rails is what’s best to build a web app, WordPress is number #1 when it comes to blogging. And these days, a great start-up must have a blog alongside its main app. So, let’s see how to add a WordPress blog to Ruby on Rails.

Why should I have WordPress on Rails?

There are several different reasons justifying why you really should have a blog, a WordPress one.

Communicate with your users

A blog -together with social media- is by far the best way to start a conversation with your users, to give them some insights or simply to communicate your latest news.

Your best ally for SEO

In today’s SEO, content is king. A blog is the best way to put a content marketing strategy in place, and having one is important for improving your Rails SEO. It enables your company to share its knowledge using the right keywords. As a result, your website is much easier to be found by users in the SERPs.

You can check out this article from KissMetrics : Why SEO Is Actually All About Content Marketing

Choose WordPress for a complete yet simple solution

A blog can definitely be built using Rails only (if you choose to go this route, take a look at Octopress). But WordPress is already so well optimized and ready to use in a few clicks, that you probably don’t need to do it on Rails.

Building your own blog solution, or using some Ruby on Rails projects, might still be the best option if you want an entire Rails integration (login, etc…). Or maybe you particularly enjoy doing it. Otherwise, and the vast majority of case, it is just unnecessary additional work. WordPress on Rails is the best solution for these cases.

You may want to read this article by the well-known ThoughtBot team, who goes through all possible solutions before actually deciding to go with their own blog solution.

Setting up a WordPress blog on Ruby on Rails in a few steps

Adding a blog to an existing website isn’t actually complicated process. But you do have to follow these steps and you can’t miss one.

Migrate your existing blog to Flywheel, or create a new one there

The blog I wanted to add to my Rails app was already existing, but hosted on a service that wasn’t convenient to use in this case.

I chose to migrate this blog to Flywheel (or you can create a blog directly there if you don’t have one). They have a very simple process to do that, and the support team is very helpful. I was concerned about moving the blog, but eventually did it without any problem.

Of course, you are free to choose another hosting service. Flywheel is the one I tested and approved but I’m sure there are other good solutions out there.

When it’s done, you will have a new blog URL (ie: yourblog.flywheelsites.com). First, you will want to redirect this URL to your Rails app.

Should I have my blog in a subdirectory (blog.mysite.com) or a subfolder (mysite.com/blog)?

Choose the subfolder. It has been proven to be better for SEO (as far as we can prove something in SEO).

Tell your choice to the Flywheel team so they can do what needs to be done on their side. They will put the settings in place to allow the WordPress site on Flywheel to load from yoursite.com. Otherwise your links will point to this WordPress blog on Flywheel.

On your Rails app

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

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

Then, you need your Rails app to redirect /blog/ to your Flywheel site. Go to config.ru and add the following code:

use Rack::ReverseProxy do
  reverse_proxy(/^\/blog(\/.*)$/,
                'http://myblog.flywheelsites.com$1',
                opts = { preserve_host: true })
 end

Note: Make sure you don’t have a trailing “/” in your URL. It should be http://myblog.flywheelsites.com$1 and not http://myblog.flywheelsites.com/$1

Last thing, in config/routes.rb, you need to redirect /blog to /blog/. Add this line:

get '/blog', to: redirect('https://mysite.com/blog/', status: 301)

Conclusion

Voilà, you have your beautiful blog alongside your powerful Ruby on Rails app.

There are a few SEO gotchas you should be aware of to avoid loosing your rankings in search engines. Mainly, you will have to redirect your articles to their new url.

You can do this simply inside config/routes.rb:

get '/my-article', to: redirect('/blog/my-article', status: 301)

There you go. I hope this article has been useful for you. I will keep it updated and add more relevant info in it. Don’t hesitate to ask me any question in the comments below.

How to add a WordPress blog to a Ruby on Rails app?
4.7 (93.33%) 9 votes

8 replies
  1. Etienne
    Etienne says:

    Hi I have a doubt, the original WordPress (where it is hosting) will have urls and the site/blog will have another url with the same content. That will create duplicate no?

    Reply
    • Codkal
      Codkal says:

      Hi Etienne,
      In over a year using it, I’ve had zero duplicate content issue (and I’m pretty sensitive when it comes to SEO).
      But if you are afraid, you can certainly implement redirections or an http auth to block the access. I do believe that’s unnecessary though.

      Reply
  2. Luke Thomas
    Luke Thomas says:

    Thanks for writing this post. I was able to set up the blog per your instructions, the only issue is that the links to articles/images/etc in the blog reference http://myblog.flywheelsites.com/$url.

    Any idea how to get that setup so it correctly references the url. I fear that Google bots will scrape that and it will lead to duplicate content issues (amongst other things).

    Reply
    • Codkal
      Codkal says:

      Hi Luke,
      I know what you mean, it’s normal during the installation phase. All you have to do is to inform the Flywheel team (through their support).
      They will put the settings in place to allow the WordPress site on Flywheel to load from yoursite.com. I’ll update the article to make this clearer.

      Reply

Trackbacks & Pingbacks

  1. […] the way, it might be a good idea to add a WordPress on your Rails app, for your […]

  2. […] his blog and/or Twitter, a coder can take time to explain and transmit coding skills to others. That’s […]

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *