简体   繁体   English

在Rails for Heroku中将URL从子目录重写为子域

[英]Rewrite URL from Subdirectory to Subdomain in Rails for Heroku

I'm trying to rewrite: 我正在尝试重写:

mysite.com/blog to blog.mysite.com mysite.com/blogblog.mysite.com

To be clear: I only want the user to see mysite.com/blog .Where the blog is actually a separately hosted wordpress site at blog.mysite.com. 需要说明的是:我只希望用户看到mysite.com/blog 。该博客实际上是位于blog.mysite.com上的单独托管的wordpress网站。 And mysite.com is a Rails app. 而且mysite.com是Rails应用程序。

I have attempted to use rack-rewrite to achieve this and I can make it work with a 301 redirect, but not with a rewrite. 我尝试使用机架重写来实现此目的,并且可以使其与301重定向一起工作,但不能与重写一起工作。

config.middleware.insert_before(Rack::Lock, Rack::Rewrite) do
  #r301   '/blog',  'http://blog.mysite.com' #works
  rewrite   '/blog',  'http://blog.mysite.com' #fails
end

The reason I am trying to do this in Rails and not in the webserver is because I am using Heroku for hosting and I believe it is not possible to configure this type of rewrite on Heroku. 我之所以尝试在Rails中而不是在Web服务器中这样做是因为我使用Heroku进行托管,并且我相信无法在Heroku上配置这种重写类型。

So my question is simply, how can I achieve this rewrite? 所以我的问题很简单,我如何实现这种重写?

ps I saw another post suggesting the use of rack-reverse-proxy but this gem seems quite old and doesn't seem to have had much development. ps:我看到另一篇文章建议使用机架反向代理,但是这个宝石看起来很老,而且似乎没有太大的发展。 Which makes me nervous of using it. 这让我不敢使用它。

In the absence of an alternative solution, I set aside my reservations and used the rack-reverse-proxy gem. 在没有其他解决方案的情况下,我搁置了保留意见,并使用了机架反向代理宝石。 As suggested in some other posts (that SO doesn't let me link to here). 正如其他一些帖子所建议的(SO不允许我链接到此处)。

It's working fine for me (Ruby 1.9.3, Rails 3.2.12). 对我来说,它工作正常(Ruby 1.9.3,Rails 3.2.12)。

My code is: 我的代码是:

use Rack::ReverseProxy do
   reverse_proxy /^\/blog(\/.*)$/, 'http://blog.example.com$1', opts={:preserve_host => true} #works
end

At the bottom of the config.ru file. 在config.ru文件的底部。

Note: In Wordpress I also needed to change the Site URL in Settings > General. 注意:在Wordpress中,我还需要在“设置”>“常规”中更改站点URL。 I changed it to: http://example.com/blog but I kept the .htaccess file unchanged. 我将其更改为: http : //example.com/blog,但未更改.htaccess文件。

A final point: all my CSS and JS were referenced OK, but my local fonts weren't. 最后一点:我所有的CSS和JS都被引用为OK,但我的本地字体却未被引用。 I solved this by referencing the fonts in the assets directory of my rails app (as opposed to in my Wordpress theme). 我通过引用Rails应用程序资产目录中的字体(而不是在Wordpress主题中)来解决此问题。

It sounds like your blog and your website are hosted in different places. 听起来您的博客和网站托管在不同的地方。

Rewrite will only work if you want to serve up the same rails page but from a different URL. 仅当您要提供相同的rails页面但使用不同的URL时,重写才有效。 It won't work between different hosts: https://github.com/jtrupiano/rack-rewrite#rewrite 它不能在不同的主机之间工作: https : //github.com/jtrupiano/rack-rewrite#rewrite

The best solution is to be OK w/ users being redirected to a subdomain. 最好的解决方案是将用户重定向到子域即可。

A couple other less ideal options: 其他一些不太理想的选择:

  • Use an iFrame of your blog in your rails app (this makes it hard to navigate and bookmark) 在Rails应用程序中使用博客的iFrame(这使导航和添加书签变得困难)
  • Serve up your blog from your rails app using something like rack-reverse-proxy (this will be slow for the user and slow down the rest of your site). 使用rails-reverse-proxy之类的东西从rails应用程序为您的博客提供服务(这对用户而言会很慢,而对网站的其余部分也会影响速度)。
  • Load your blog via JavaScript after the Rails page loads (SEO won't be good and will probably be hard to navigate) 在Rails页面加载后通过JavaScript加载博客(SEO不好,可能很难导航)

So, I recommend just using the subdomain. 因此,我建议仅使用子域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM