如何使用子目录而不是子域? [英] How can I use a subdirectory instead of a subdomain?

查看:24
本文介绍了如何使用子目录而不是子域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个 rails 应用程序,我将在域.com 上的 Heroku 上托管该应用程序.我想将 WordPress 用于 phpfog 上托管的博客,但我不想使用像 blog.domain.com 这样的子域.我宁愿使用像 domain.com/blog 之类的子目录

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

这与 SEO 无关……我只是不喜欢子域.子目录更性感(是的...我确实说过).

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.

推荐答案

您可以使用 rack-reverse-proxy gem neezer 发现这样做.首先你需要添加 gem "rack-reverse-proxy", :require =>rack/reverse_proxy" 到您的 Gemfile 并运行 bundle install.接下来,您将修改您的 config.ru 以将 /blog/ 路由转发到您想要的博客:

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

您可能已经有了第一个 require 语句和 run YourAppName... 语句.有几个重要的细节使这项工作有效.

You probably already have the first require statement and the run YourAppName... statement. There are a couple important details that make this work.

首先,当您添加所需的博客 URL 时,不能在其上保留尾部斜杠.如果这样做,当有人请求 http://yourdomain.com/blog/ 时,gem 会将他们转发到 http://you.yourbloghost.com//一个额外的斜线.

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.

其次,如果 :preserve_host 选项未启用,您的博客托管服务器将看到该请求是针对 http://yourdomain.com/blog/而不是 http://you.yourbloghost.com 并且会返回错误的结果.

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.

如果博客使用 /absolute/paths/to/images/,您的 CSS 或图像可能仍然存在问题.

You still may have an issue with the CSS or images if the blog uses /absolute/paths/to/images/.

这篇关于如何使用子目录而不是子域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆