Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use a subdirectory instead of a subdomain?

Tags:

I'm building a rails app that I'll host on Heroku at domain.com. And I'd like to use WordPress for the blog hosted on phpfog, but I don't want to use a subdomain like blog.domain.com. I'd instead prefer to use a subdirectory like domain.com/blog

Its not about SEO...I'm just not a fan of subdomains. Subdirectories are sexier (yeah...I actually said that).

Any idea on how I can reliably accomplish this? Thanks in advance for the help.

like image 893
Eddie Avatar asked May 30 '11 17:05

Eddie


1 Answers

You can use the rack-reverse-proxy gem that neezer found to do this. First you'll want to add gem "rack-reverse-proxy", :require => "rack/reverse_proxy" to your Gemfile and run bundle install. Next, you'll modify your config.ru to forward the /blog/ route to your desired blog:

require ::File.expand_path('../config/environment',  __FILE__)  use Rack::ReverseProxy do          reverse_proxy /^\/blog(\/.*)$/, 'http://notch.tumblr.com$1', opts={:preserve_host => true} end  run YourAppName::Application 

You probably already have the first require statement and the run YourAppName... statement. There are a couple important details that make this work.

First, when you add your desired blog URL, you can't keep the trailing slash on it. If you do, when someone requests http://yourdomain.com/blog/, the gem will forward them to http://you.yourbloghost.com// with an extra trailing slash.

Secondly, if the :preserve_host option isn't enabled, your blog hosting server will see the request as being for http://yourdomain.com/blog/ instead of as http://you.yourbloghost.com and will return bad results.

You still may have an issue with the CSS or images if the blog uses /absolute/paths/to/images/.

like image 89
jplewicke Avatar answered Sep 30 '22 18:09

jplewicke