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

3
我有一个在Heroku上的Rails应用和一个单独托管在GoDaddy上的WordPress博客。我该如何将RailsApp.com/blog路由到WordPress,并保持它作为应用程序的子目录?
2个回答

3

如果您正在使用Rails3.1+ (+Rails4)版本,则需要按照以下步骤进行操作:

首先,在Gemfile中添加gem rack-proxy:

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');

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/")

我非常了解那篇文章,尽管它适用于基于Heroku的WordPress。这是我尝试解决问题的一部分,我可以让博客显示在正确的子目录中,但是当您单击博客上的任何内容时,它会切换到博客的域名。 - user2469227
2
为了解决这个问题,我在WordPress上将站点地址URL更改为mywebsite.com/blog,并在.htaccess中添加了rewritebase /blog/。对于开箱即用的WordPress来说,这非常完美,因为文章是通过像/?p=1这样的扩展名进行组织的,其中这个链接指向第一篇文章。我正在尝试使用已经大幅修改过的博客,其帖子是按照blogurl.com/name-of-article进行组织的,但出现了问题,因为当我尝试相同的解决方案时,从主页点击帖子会将我带到我的站点根目录而不是访问帖子。 - user2469227
请注意,“rack-reverse-proxy”不适用于生产系统。在生产中最好使用您的Web服务器(如Nginx或Apache)来配置代理设置。 - Eric Terry

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接