我如何使用子目录而不是子域

我如何使用子目录而不是子域

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

问题描述

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



它不是关于SEO ......我只是不喜欢子域名迷。子目录更性感(是的...我实际上是这么说的)。



关于我如何可靠地完成此任务的任何想法?您可以使用。首先,您需要添加 gemrack-reverse-proxy,:require => rack / reverse_proxy到您的Gemfile并运行 bundle install 。接下来,您将修改 config.ru 以将 / 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语句,运行YourAppName ... 语句。有几个重要的细节使这项工作。

首先,当您添加所需的博客网址时,无法保留最后的斜杠。如果是这样,当有人请求 http://yourdomain.com/blog/ 时,gem会将它们转发给 http://you.yourbloghost .com // ,并带有一个额外的斜线。第二,如果:preserve_host 选项未启用,您的博客托管服务器将看到请求为为 http://yourdomain.com/blog/ 而不是 http://you.yourbloghost.com 和会返回不好的结果。

如果博客使用 / absolute / paths / to / images /


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.

解决方案

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

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 to http://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 for http://yourdomain.com/blog/ instead of as http://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/.

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

07-31 22:04