Redirect non www to www via routes.rb in rails
In routes.rb:
if Rails.env.production?
def query_params_to_query(request)
query_params = request.params.except(:path, :format, :subdomain)
query_params.any? ? "?#{query_params.to_query}" : ""
end
constraints(:subdomain => '') do
redirect_action = ->(params, _request) do
"https://www.example.com/#{params[:path]}#{query_params_to_query(_request)}"
end
root to: redirect(redirect_action)
get '/*path', to: redirect(redirect_action)
end
end
if Rails.env.production?
def query_params_to_query(request)
query_params = request.params.except(:path, :format, :subdomain)
query_params.any? ? "?#{query_params.to_query}" : ""
end
constraints(:subdomain => '') do
redirect_action = ->(params, _request) do
"https://www.example.com/#{params[:path]}#{query_params_to_query(_request)}"
end
root to: redirect(redirect_action)
get '/*path', to: redirect(redirect_action)
end
end
Comments
Post a Comment