Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'.

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>
like image 719
Dan Crews Avatar asked Nov 01 '22 07:11

Dan Crews


1 Answers

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.

like image 88
Mathew Tinsley Avatar answered Nov 15 '22 07:11

Mathew Tinsley