Route WordPress Blog to Rails Subfolder – Blog Links Not Changed

I currently have a Rails app, my_app.com, and an associated WordPress blog, blog.my_app.com. They are both running independently on Heroku (i.e. no Apache or Nginx scripting)

I am trying to move the blog into a sub-folder of the Rails app, my_app.com/blog without losing any of the blog’s existing SEO ‘juice’.

Read More

I’ve implemented the rack-reverse-proxy gem and the blog’s homepage dutifully appears at http://my_app.com/blog/ as desired. However, all of the links embedded in the blog still point to the sub-domain rather than the app’s blog folder.

How do I configure the blog’s links to http://my_app.com/blog/post1 as opposed to blog.my_app.com/post1??

My config.ru file:

require ::File.expand_path('../config/environment',  __FILE__)

use Rack::ReverseProxy do 
  reverse_proxy(/^/blog(/.*)$/, 'http://my-blog.herokuapp.com$1', opts = {:preserve_host => true})
end

use Rack::Deflater

run MyBlog::Application

In my routes.rb:

constraints domain: 'blog.my_app.com' do 
  get '(*path)' => 'application#blog'
end

get "/blog" => redirect("/blog/")

In my ApplicationController:

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

My WP .htaccess file:

<IfModule mod_rewrite.c>
RewriteEngine  On
RewriteBase /blog/
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Related posts

Leave a Reply

3 comments

  1. It sounds like all you need to do is update the URLs in your WordPress site to use the new scheme. The easiest way to do this would be to perform a search and replace in the database:

    Search: blog.my_app.com
    Replace: my_app.com/blog
    

    However there is one caveat. WordPress stores a lot of serialized data in the database. Serialized strings have a defined length so just changing the strings without updating the length will cause problems.

    I like to use this utility to update links in WordPress. It will perform a search and replace on the database and it will properly update serialized data. Make sure you have a backup of your database in case things go wrong.

  2. If you’re talking about links that you manually entered, then mtinsley’s answer is correct. However if you’re talking about links that are auto-generated by WordPress, then the answer is even easier! You just have to go into the WordPress admin and change the Site URL to http://my_app.com/blog/.

    HOWEVER, there is a big caveat with this. You are trying to bring along your “link juice”. If blog.my_app.com/some/post doesn’t redirect to my_app.com/blog/some/post then you will not keep your link juice.

    To fix that, you will want to set up htaccess rules or find a WordPress plugin that automatically creates redirects from blog.my_app.com/some/post to my_app.com/blog/some/post. Also, make sure that blog.my_app.com continues to resolve to your blog site during the transition.

  3. Actually, this is one of the things that annoys me most about WordPress. It’s really not designed to be moved. There are settings and internal links scattered all over the database that are specified absolutely instead of relative to the WordPress base. This makes it a pain in the ass when you want to do something every professional website should do: set up a staging instance, and move it to the live server only when you have everything set up right.

    Fortunately, there’s an excellent tool to solve this problem:
    https://interconnectit.com/products/search-and-replace-for-wordpress-databases/

    It does a database-wide search and replace that automatically solves the problem with PHP serialized values that arises if you try to do it on your own. You can restrict it to only certain tables, and you can preview the changes it will make before executing them. Whenever you move a WordPress installation, use this tool to search for the old URL and replace it with the new one.