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

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

    If you want to redirect from the top-level domain (TLD) to the www subdomain, use this code:

    constraints :subdomain => '' do
      match '(*any)' => redirect { |p, req| req.url.sub('//', '//www.') }
    end
    

    Note: This code the use of sub, not gsub, because sub replaces the first occurrence of the double-slashes where gsub would replace all double-slashes.

提交回复
热议问题