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

时间:2022-07-29 21:14:49

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

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

Its not about SEO...I'm just not a fan of subdomains. Subdirectories are sexier (yeah...I actually said that).

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

Any idea on how I can reliably accomplish this? Thanks in advance for the help.

关于如何可靠地实现这一目标的任何想法?先谢谢您的帮助。

4 个解决方案

#1


18  

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:

您可以使用neezer发现的rack-reverse-proxy gem来执行此操作。首先,您需要在您的Gemfile中添加gem“rack-reverse-proxy”,:require =>“rack / reverse_proxy”并运行bundle install。接下来,您将修改config.ru以将/ blog / route转发到您想要的博客:

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.

您可能已经有了第一个require语句和运行YourAppName ...语句。有一些重要的细节使这项工作。

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.

首先,当您添加所需的博客URL时,您无法在其上保留尾随斜杠。如果你这样做,当有人请求http://yourdomain.com/blog/时,gem会将它们转发到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.

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

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

如果博客使用/ absolute / paths / to / images /,您仍可能遇到CSS或图像问题。

#2


3  

I'd say your best bet is to try and do a reverse proxy with Rack middleware (akin to Apache's mod_proxy).

我想说你最好的办法就是尝试使用Rack中间件(类似于Apache的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 website your-phpfog-account.com/wordpress-installation.

一个快速的谷歌搜索揭示了这个宝石(https://github.com/jaswope/rack-reverse-proxy),但作者提到它可能没有生产就绪。拥有Rack中间件代理应该允许您将子域yourdomain.com/blog转发到另一个网站your-phpfog-account.com/wordpress-installation。

#3


1  

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.

据我所知,如果可以使用重写规则,则无法使用heroku访问Apache配置文件。

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.

如果您选择不使用heroku,您可以随时执行我在下面详述的内容。但是,如果您不使用heroku,您可以轻松地将wordpress提取到/ public / rails文件夹,并再次使用重写规则来获取apache处理博客请求。

In your apache configuration you'll need to add.

在您的apache配置中,您需要添加。

RewriteRule ^/blog/?(.*)$ http://somedomain.com/~user/blog/$1 [P,NC,QSA,L]

It will redirect all requests to /blog/ to the other server.

它会将所有请求重定向到/ blog /到其他服务器。

Source: http://www.igvita.com/2007/07/04/integrating-wordpress-and-rails/

资料来源:http://www.igvita.com/2007/07/04/integrating-wordpress-and-rails/

#4


0  

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:

除了jplewickeless'答案,我最终编写自定义Rack middelware来替换反向代理一侧的绝对URL和其他路径。本指南将帮助您开始:

http://railscasts.com/episodes/151-rack-middleware

http://railscasts.com/episodes/151-rack-middleware

#1


18  

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:

您可以使用neezer发现的rack-reverse-proxy gem来执行此操作。首先,您需要在您的Gemfile中添加gem“rack-reverse-proxy”,:require =>“rack / reverse_proxy”并运行bundle install。接下来,您将修改config.ru以将/ blog / route转发到您想要的博客:

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.

您可能已经有了第一个require语句和运行YourAppName ...语句。有一些重要的细节使这项工作。

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.

首先,当您添加所需的博客URL时,您无法在其上保留尾随斜杠。如果你这样做,当有人请求http://yourdomain.com/blog/时,gem会将它们转发到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.

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

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

如果博客使用/ absolute / paths / to / images /,您仍可能遇到CSS或图像问题。

#2


3  

I'd say your best bet is to try and do a reverse proxy with Rack middleware (akin to Apache's mod_proxy).

我想说你最好的办法就是尝试使用Rack中间件(类似于Apache的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 website your-phpfog-account.com/wordpress-installation.

一个快速的谷歌搜索揭示了这个宝石(https://github.com/jaswope/rack-reverse-proxy),但作者提到它可能没有生产就绪。拥有Rack中间件代理应该允许您将子域yourdomain.com/blog转发到另一个网站your-phpfog-account.com/wordpress-installation。

#3


1  

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.

据我所知,如果可以使用重写规则,则无法使用heroku访问Apache配置文件。

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.

如果您选择不使用heroku,您可以随时执行我在下面详述的内容。但是,如果您不使用heroku,您可以轻松地将wordpress提取到/ public / rails文件夹,并再次使用重写规则来获取apache处理博客请求。

In your apache configuration you'll need to add.

在您的apache配置中,您需要添加。

RewriteRule ^/blog/?(.*)$ http://somedomain.com/~user/blog/$1 [P,NC,QSA,L]

It will redirect all requests to /blog/ to the other server.

它会将所有请求重定向到/ blog /到其他服务器。

Source: http://www.igvita.com/2007/07/04/integrating-wordpress-and-rails/

资料来源:http://www.igvita.com/2007/07/04/integrating-wordpress-and-rails/

#4


0  

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:

除了jplewickeless'答案,我最终编写自定义Rack middelware来替换反向代理一侧的绝对URL和其他路径。本指南将帮助您开始:

http://railscasts.com/episodes/151-rack-middleware

http://railscasts.com/episodes/151-rack-middleware