2

I'm trying to rewrite:

mysite.com/blog to blog.mysite.com

To be clear: I only want the user to see mysite.com/blog .Where the blog is actually a separately hosted wordpress site at blog.mysite.com. And mysite.com is a Rails app.

I have attempted to use rack-rewrite to achieve this and I can make it work with a 301 redirect, but not with a rewrite.

config.middleware.insert_before(Rack::Lock, Rack::Rewrite) do
  #r301   '/blog',  'http://blog.mysite.com' #works
  rewrite   '/blog',  'http://blog.mysite.com' #fails
end

The reason I am trying to do this in Rails and not in the webserver is because I am using Heroku for hosting and I believe it is not possible to configure this type of rewrite on Heroku.

So my question is simply, how can I achieve this rewrite?

p.s. I saw another post suggesting the use of rack-reverse-proxy but this gem seems quite old and doesn't seem to have had much development. Which makes me nervous of using it.

andyleesuk
  • 110
  • 10

2 Answers2

2

In the absence of an alternative solution, I set aside my reservations and used the rack-reverse-proxy gem. As suggested in some other posts (that SO doesn't let me link to here).

It's working fine for me (Ruby 1.9.3, Rails 3.2.12).

My code is:

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

At the bottom of the config.ru file.

Note: In Wordpress I also needed to change the Site URL in Settings > General. I changed it to: http://example.com/blog but I kept the .htaccess file unchanged.

A final point: all my CSS and JS were referenced OK, but my local fonts weren't. I solved this by referencing the fonts in the assets directory of my rails app (as opposed to in my Wordpress theme).

andyleesuk
  • 110
  • 10
  • do you also have a route set up? I get `no route matches` – Trip Feb 16 '15 at 11:44
  • 1
    no - I do have anything in routes relating to this. Maybe check rack-reverse-proxy is installed and working by going back to the basic example on their page: https://github.com/jaswope/rack-reverse-proxy#usage – andyleesuk Feb 16 '15 at 15:42
1

It sounds like your blog and your website are hosted in different places.

Rewrite will only work if you want to serve up the same rails page but from a different URL. It won't work between different hosts: https://github.com/jtrupiano/rack-rewrite#rewrite

The best solution is to be OK w/ users being redirected to a subdomain.

A couple other less ideal options:

  • Use an iFrame of your blog in your rails app (this makes it hard to navigate and bookmark)
  • Serve up your blog from your rails app using something like rack-reverse-proxy (this will be slow for the user and slow down the rest of your site).
  • Load your blog via JavaScript after the Rails page loads (SEO won't be good and will probably be hard to navigate)

So, I recommend just using the subdomain.

Josh
  • 8,329
  • 4
  • 36
  • 33
  • Thanks for taking the time to respond Josh. I'm keen to keep everything on the same subdomain for SEO purposes. Even though Google says it doesn't matter, [others say it does](http://moz.com/community/q/moz-s-official-stance-on-subdomain-vs-subfolder-does-it-need-updating). In the end, despite my reservations I managed to used rack-reverse-proxy successfully. I'm about to update/answer it to that effect. – andyleesuk Sep 17 '14 at 09:29