重定向“myapp.com” 至“www.myapp.com” 在 Rails 中不使用 htaccess?

发布于 2024-07-10 01:39:13 字数 471 浏览 4 评论 0原文

使用 Morph Labs 的 Appspace 部署站点意味着无法自动将“myapp.com”重定向到“www.myapp.com”(并且无法访问 .htacess)。

有没有一种轨道内的方法可以做到这一点? 我需要像 subdomain-fu 这样的插件吗?

更具体地说,我正在尝试执行以下操作:

  • 'myapp.com' => 'www.myapp.com'
  • 'myapp.com/session/new' => 'www.myapp.com/session/new'

基本上,我总是希望在每个请求前面加上 'www' 子域(因为 SSL 证书特别有一个通用名称 'www.myapp.com')。

Using Morph Labs' Appspace to deploy a site means no automated way to redirect 'myapp.com' to 'www.myapp.com' (and no access to .htacess).

Is there an in-rails way to do this? Would I need a plugin like subdomain-fu?

More specifically, I'm trying to do something like:

  • 'myapp.com' => 'www.myapp.com'
  • 'myapp.com/session/new' => 'www.myapp.com/session/new'

Basically, I always want the 'www' subdomain prepended on every request (because the SSL cert specifically has a common name of 'www.myapp.com').

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

街道布景 2024-07-17 01:39:16

我知道这个问题已经得到解答,但我认为其他人都应该了解 CodeRack:Canonical Host 解决方案。 这真的很好,因为它允许特定于环境的重定向。 http://coderack.org/users/tylerhunt/middlewares/6-canonical-主机

I know this is answered, but I thought everyone else should know about the CodeRack: Canonical Host solution. This is really nice as it allows for env specific redirects. http://coderack.org/users/tylerhunt/middlewares/6-canonical-host

花间憩 2024-07-17 01:39:16

这里有几种不同的方法:

 head :moved_permanently, :location => ‘http://www.newdomain.com’

另一种:

def rails_301
headers["Status"] = "301 Moved Permanently"
redirect_to "http://www.newdomain.com"
end 

Here is a couple of different ways:

 head :moved_permanently, :location => ‘http://www.newdomain.com’

another:

def rails_301
headers["Status"] = "301 Moved Permanently"
redirect_to "http://www.newdomain.com"
end 
陌伤ぢ 2024-07-17 01:39:16

对于那些希望使用 Heroku 强制使用 SSL 的人来说,这对我来说效果很好,基于 根域上的 Heroku SSL

在我的 DNS 设置中,我设置了一个 URL/转发记录(DNS 简单)

URL foo.com     3600        http://www.foo.com

CNAME 设置只需要为 WWW 设置

CNAME   www.foo.com 3600        providedssslendpoint.herokussl.com

我还必须为我的根设置和别名

ALIAS   foo.com 3600        providedsslendpoint.herokussl.com

然后我决定简单地将 foo.com 替换为环境变量 ENV['SITE_HOST'] (其中 SITE_HOST 可能等于 www.foo.com 或 test.foo.com),以便我可以进行控制通过我的 heroku 配置。 这样,我就可以控制不同环境中发生的事情。 (要在本地设置环境变量,请参阅 https://github.com/bkeepers/dotenv

) ,我的测试应用程序使用 test.foo.com 作为 url,它也有自己的 SSL 端点,因此对我来说效果很好。

  before_filter :check_domain

  def check_domain
    if Rails.env.production? || Rails.env.testing? and request.host.downcase != ENV['SITE_HOST']
      redirect_to request.protocol + ENV['SITE_HOST'] + request.fullpath, :status => 301
    end
  end

从现在开始,最终用户将始终使用强制 SSL 访问 www。 旧的链接会出现轻微的挂起,但不会引起任何明显的影响。

For those of you who are looking to also force SSL using heroku, this worked well for me, based on Heroku SSL on root domain

In my DNS settings I set up a URL / Forward record (DNS Simple)

URL foo.com     3600        http://www.foo.com

The CNAME setup only needs to be setup for WWW

CNAME   www.foo.com 3600        providedssslendpoint.herokussl.com

I also had to setup and Alias for my root

ALIAS   foo.com 3600        providedsslendpoint.herokussl.com

Then I decided to simply replace foo.com with an env variable ENV['SITE_HOST'] (where SITE_HOST might equal www.foo.com or test.foo.com) so I can have control via my heroku configuration. That way, I can control what happens in different environments. (for setting up env variables locally see https://github.com/bkeepers/dotenv)

For example, my test app uses test.foo.com as the url it also has its own SSL endpoint so that works fine for me.

  before_filter :check_domain

  def check_domain
    if Rails.env.production? || Rails.env.testing? and request.host.downcase != ENV['SITE_HOST']
      redirect_to request.protocol + ENV['SITE_HOST'] + request.fullpath, :status => 301
    end
  end

From now on, end users will always access www with forced SSL. Old links will suffer a small hang but nothing noticeable.

傲性难收 2024-07-17 01:39:15

我必须更改 Carson 的答案才能使其在 Rails 3 中工作。我将 request.uri 替换为 request.fullpath:

class ApplicationController < ActionController::Base
  protect_from_forgery

  Rails.env.production? do
    before_filter :check_url
  end

  def check_url
    redirect_to request.protocol + "www." + request.host_with_port + request.fullpath if !/^www/.match(request.host)
  end
end

I had to change Carson's answer to get this to work in Rails 3. I replaced request.uri with request.fullpath:

class ApplicationController < ActionController::Base
  protect_from_forgery

  Rails.env.production? do
    before_filter :check_url
  end

  def check_url
    redirect_to request.protocol + "www." + request.host_with_port + request.fullpath if !/^www/.match(request.host)
  end
end
任性一次 2024-07-17 01:39:15

这对我来说非常有用。 我确实做了一点补充,因为我只希望在生产环境中出现这种行为:

def check_uri
  redirect_to request.protocol + "www." + request.host_with_port + request.request_uri if !/^www/.match(request.host) if Rails.env == 'production'
end

This worked great for me. I did make one small addition as I only wanted this behavior in my production environment:

def check_uri
  redirect_to request.protocol + "www." + request.host_with_port + request.request_uri if !/^www/.match(request.host) if Rails.env == 'production'
end
一身软味 2024-07-17 01:39:14

卡森的回答非常有效。

这是相反的代码(www -> 无 www)

before_filter :check_uri

def check_uri
  if /^www/.match(request.host)
    redirect_to request.protocol + request.host_with_port[4..-1] + request.request_uri 
  end
end

Carson's answer works great.

Here's the code to go the other way (www -> no www)

before_filter :check_uri

def check_uri
  if /^www/.match(request.host)
    redirect_to request.protocol + request.host_with_port[4..-1] + request.request_uri 
  end
end
过期以后 2024-07-17 01:39:14

也许这样的事情可以解决问题:

class ApplicationController < ActionController::Base
  before_filter :check_uri

  def check_uri
    redirect_to request.protocol + "www." + request.host_with_port + request.request_uri if !/^www/.match(request.host)
  end
end

Maybe something like this would do the trick:

class ApplicationController < ActionController::Base
  before_filter :check_uri

  def check_uri
    redirect_to request.protocol + "www." + request.host_with_port + request.request_uri if !/^www/.match(request.host)
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文