How do I redirect without www using Rails 3 / Rack?

前端 未结 9 2085
北荒
北荒 2020-12-12 19:36

I understand there are a lot of questions that answer this. I\'m familiar with .htaccess and nginx.conf methods, but I do not have access to such t

9条回答
  •  伪装坚强ぢ
    2020-12-12 20:06

    For Rails 4 the above solutions have to be appended with the Verb construction e.g. via: [:get, :post]. Duke's solution becomes:

      constraints(:host => /^www\./) do
        match "(*x)" => redirect { |params, request|
          URI.parse(request.url).tap {|url| url.host.sub!('www.', '') }.to_s
        }, via: [:get, :post]
      end
    

提交回复
热议问题