Here is my Ruby on Rails code to redirect a request from https://example.com
to https://www.example.com
:
class ApplicationController < ActionController::Base
before_filter :add_www_subdomain
private
def add_www_subdomain
if Rails.env.production?
unless /^www/.match(request.host)
redirect_to("#{request.protocol}www.#{request.host_with_port}",status: 301)
end
end
end
end
Now the issue is, when someone lands at https://example.com/product/abc
they are being redirected to https://www.example.com
supposedly, they should go to https://www.example.com/product/abc
. Is there any trick for this? Thanks