将现有的WordPress博客设为Rails应用程序的子目录?

时间:2013-06-26 18:28:15

标签: ruby-on-rails wordpress ruby-on-rails-3

我在Heroku上有一个Rails应用程序,在GoDaddy上有一个单独的WordPress博客。如何将RailsApp.com/blog路由到WordPress,同时将其保存为应用程序的子目录?

2 个答案:

答案 0 :(得分:3)

对于Rails3.1 +(+ Rails4),你应该遵循以下: 首先,将gem rack-proxy添加到Gemfile

gem" rack-proxy"

然后将文件proxy.rb添加到Rails.root / lib中,如下所示:

require 'rack/proxy'
class Proxy < Rack::Proxy
  def initialize(app)
    @app = app
  end
  def call(env)
    original_host = env["HTTP_HOST"]
    rewrite_env(env)
    if env["HTTP_HOST"] != original_host
      perform_request(env)
    else
      # just regular
      @app.call(env)
    end
  end
  def rewrite_env(env)
    request = Rack::Request.new(env)
    if request.path =~ /^\/blog(\/.*)$/
      # enable these code if you set blog as ROOT directory in wordpress folder
      # env['REQUEST_PATH'] = '/'
      # env['ORIGINAL_FULLPATH'] = '/'
      # env['PATH_INFO'] = '/' # set root path request
      env['REQUEST_URI'] = 'http://tinle1201.wordpressdomain.com' # your path
      env["SERVER_PORT"] = 80
      env["HTTP_HOST"] = "tinle1201.wordpressdomain.com" # point to your host
    end
    env
  end
end*

将代理注册到中间件到config / application.rb:

require File.expand_path('../boot', __FILE__)
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env)
module AdultSavings
  class Application < Rails::Application
    config.autoload_paths += %W(#{config.root}/lib)
    config.middleware.use "Proxy"
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.
    # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
    # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
    # config.time_zone = 'Central Time (US & Canada)'
    # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
    # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
    # config.i18n.default_locale = :de
  end
end*

将此行添加到php文件wp-config.php中:

define('WP_HOME', 'http://tinle1201.wordpressdomain.com/blog');

答案 1 :(得分:0)

Rails配置

添加到Gemfile:

gem "rack-reverse-proxy", :require => "rack/reverse_proxy"

现在我们希望将/ blog和/ blog /从Rails应用程序定向到Wordpress实例。

在运行应用程序之前将其添加到您的config.ru:

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

在config / routes.rb中添加路由:

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