这是一个简单的问题,但我似乎无法通过快速谷歌搜索找到答案。
Ruby on Rails 直接执行 301 的方式是什么(http://x.com/abc http://x.com/abc > http://www.x.com/abc http://www.x.com/abc). A before_filter
?
理想情况下,您应该在 Web 服务器(Apache、nginx 等)配置中执行此操作,以便请求根本不会触及 Rails。
添加以下内容before_filter
给你的ApplicationController
:
class ApplicationController < ActionController::Base
before_filter :add_www_subdomain
private
def add_www_subdomain
unless /^www/.match(request.host)
redirect_to("#{request.protocol}x.com#{request.request_uri}",
:status => 301)
end
end
end
如果您确实想使用 Apache 进行重定向,您可以使用以下命令:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.x\.com [NC]
RewriteRule ^(.*)$ http://www.x.com/$1 [R=301,L]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)