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

前端 未结 9 2129
北荒
北荒 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:03

    I really like using the Rails Router for such things. Previous answers were good, but I wanted something general purpose I can use for any url that starts with "www".

    I think this is a good solution:

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

提交回复
热议问题