Redirect non-www requests to www URLs in Ruby on Rails

前端 未结 7 2006
無奈伤痛
無奈伤痛 2020-12-01 09:52

It is a simple issue, but I can\'t seem to find an answer doing some quick googling.

What\'s the Ruby on Rails way of doing this 301 direct (http://x.com/abc > http:

7条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-01 10:36

    For rails 4, use it -

      before_filter :add_www_subdomain
    
      private
      def add_www_subdomain
        unless /^www/.match(request.host)
          redirect_to("#{request.protocol}www.#{request.host_with_port}",status: 301)
        end
      end
    

提交回复
热议问题