【问题标题】:Rails 301 RedirectionRails 301 重定向
【发布时间】:2012-04-20 18:48:07
【问题描述】:

我想将多个条目重定向到我的登录页面到一个 URL。

以下网址 http://mysite.comhttp://www.mysite.com 将使用 301 重定向重定向到 http://www.mysite.com/。如何/应该在 Rails 中做到这一点?

【问题讨论】:

  • 您使用的是哪个网络服务器?

标签: ruby-on-rails redirect seo http-status-code-301


【解决方案1】:

在 Rails 中使用 301 重定向

class RedirectController < ApplicationController
  def index
    redirect_to :root, :status => :moved_permanently
  end
end

【讨论】:

    【解决方案2】:

    我使用了这个post 并决定使用非 www 网址进行重定向。

    【讨论】:

      【解决方案3】:

      我没有尝试过,但是这样的东西应该可以工作:

      class ApplicationController < ActionController::Base
        before_filter :correct_domain!
      
        private
        def correct_domain!
          unless request.host == 'www.mysite.com'
            redirect_to root_url, :status => 301  # or explicitly 'http://www.mysite.com/'
          end
        end
      end
      

      但我不确定主机属性中是否存在斜杠...

      【讨论】:

      • 感谢 Vapire,但是当我将它添加到我的应用控制器时,我收到了重定向循环错误。
      • 需要在家庭控制器中完成,对我来说效果很好。
      • @Dru 只是一个猜测,但您的 root_url 可能与您的 request.host 不匹配(即不包括 www)。
      猜你喜欢
      • 2011-11-17
      • 1970-01-01
      • 1970-01-01
      • 2016-08-25
      • 1970-01-01
      • 2012-03-13
      • 1970-01-01
      • 2010-09-26
      • 2013-01-07
      相关资源
      最近更新 更多