gpt4 book ai didi

ruby-on-rails - 将现有的 WordPress 博客作为 Rails 应用程序的子目录?

转载 作者:行者123 更新时间:2023-12-04 05:59:26 25 4
gpt4 key购买 nike

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

最佳答案

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

关于ruby-on-rails - 将现有的 WordPress 博客作为 Rails 应用程序的子目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17327761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com