How can I use a subdirectory instead of a subdomain?

前端 未结 4 339
予麋鹿
予麋鹿 2020-12-04 20:14

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 bl

相关标签:
4条回答
  • 2020-12-04 20:47

    I'd say your best bet is to try and do a reverse proxy with Rack middleware (akin to Apache's mod_proxy).

    A quick Google search revealed this gem ( https://github.com/jaswope/rack-reverse-proxy ), but the author mentions that it's probably not production-ready. Having a Rack middleware proxy should allow you to forward your subdomain yourdomain.com/blog to another website your-phpfog-account.com/wordpress-installation.

    0 讨论(0)
  • 2020-12-04 20:48

    In addition to jplewickeless' answer, I ended up writing custom Rack middelware to replace absolute urls and other paths at the side of the reverse proxy. This guide will get you started on that:

    http://railscasts.com/episodes/151-rack-middleware

    0 讨论(0)
  • 2020-12-04 20:53

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

    0 讨论(0)
  • 2020-12-04 20:57

    As far as I can tell you can't access the Apache config file with heroku if you could you could use a Rewrite rule.

    If you choose not to use heroku you can always do what I detail below.. However if you're not using heroku you could just as easily extract wordpress to the /public/ rails folder and once again use a rewrite rule to get apache to handle the blog requests.

    In your apache configuration you'll need to add.

    RewriteRule ^/blog/?(.*)$ http://somedomain.com/~user/blog/$1 [P,NC,QSA,L]
    

    It will redirect all requests to /blog/ to the other server.

    Source: http://www.igvita.com/2007/07/04/integrating-wordpress-and-rails/

    0 讨论(0)
提交回复
热议问题