如何使用子目录而不是子域?
我正在构建一个 Rails 应用程序,并将其托管在 Heroku 的 domain.com 上。我想将 WordPress 用于 phpfog 上托管的博客,但我不想使用像 blog.domain.com 这样的子域。相反,我更喜欢使用像domain.com/blog 这样的子目录,
这与 SEO 无关……我只是不喜欢子域。子目录更性感(是的......我实际上就是这么说的)。
关于如何可靠地实现这一点有什么想法吗?预先感谢您的帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 neezer 发现的 rack-reverse-proxy gem 来执行此操作。首先,您需要添加 gem "rack-reverse-proxy", :require => “rack/reverse_proxy” 到您的 Gemfile 并运行
bundle install
。接下来,您将修改config.ru
以将/blog/
路由转发到您想要的博客:您可能已经有了第一个 require 语句和
run YourAppName...
语句。有几个重要的细节使这项工作得以实现。首先,当您添加所需的博客 URL 时,不能保留末尾的斜杠。如果您这样做,当有人请求
http://yourdomain.com/blog/
时,gem 会将其转发到http://you.yourbloghost.com//
额外的尾部斜杠。其次,如果未启用
:preserve_host
选项,您的博客托管服务器会将请求视为针对http://yourdomain.com/blog/
而不是http://you.yourbloghost.com
并将返回不良结果。如果博客使用
/absolute/paths/to/images/
,您仍然可能遇到 CSS 或图像问题。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 runbundle install
. Next, you'll modify yourconfig.ru
to forward the/blog/
route to your desired blog: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 tohttp://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 forhttp://yourdomain.com/blog/
instead of ashttp://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/
.我想说你最好的选择是尝试使用 Rack 中间件(类似于 Apache 的
mod_proxy
)进行反向代理。快速的谷歌搜索发现了这个宝石( https://github.com/jaswope/rack-reverse-proxy ),但作者提到它可能还没有准备好生产。拥有 Rack 中间件代理应该允许您将子域
yourdomain.com/blog
转发到另一个网站your-phpfog-account.com/wordpress-installation
。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 websiteyour-phpfog-account.com/wordpress-installation
.据我所知,如果可以的话,您无法使用 Heroku 访问 Apache 配置文件,您可以使用重写规则。
如果您选择不使用heroku,您可以随时执行我在下面详细说明的操作。但是,如果您不使用heroku,您可以轻松地将wordpress提取到/public/rails文件夹,然后再次使用重写规则让apache处理博客请求。
您需要在您的 apache 配置中添加。
它将所有对 /blog/ 的请求重定向到其他服务器。
来源:http://www.igvita.com/2007/ 07/04/集成-wordpress-and-rails/
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.
It will redirect all requests to /blog/ to the other server.
Source: http://www.igvita.com/2007/07/04/integrating-wordpress-and-rails/
除了 jplewickeless 的答案之外,我最终还编写了自定义 Rack middelware 来替换反向代理一侧的绝对 url 和其他路径。本指南将帮助您开始:
http://railscasts.com/episodes/151-rack-中间件
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