I have a Rails 2.3.2 Application hosted on Heroku.
The naked domain and the www subdomain were added years ago and they both have some authority for search engines.
However the problem is that search engines are seeing the content as duplicate so I want to do 301 redirect from the naked domain to the www subdomain.
I have asked Zerigo DNS if I can do this via their platform and they said no, that I have to do it on Heroku's side. Heroku does not allow access to the server so I need some Rack middleware to perform this.
According to this answer here the gem rack-rewrite can do this.
However I really am a NOOB about this and I don't know how to set this up so that any url from the naked domain will be 301 redirected to the www subdomain.
Example:
example.com/products/12 => www.example.com/products/12
I am currently doing the wrong things in the application controller as follows:
before_filter :ensure_domain
protected
def ensure_domain
app_domain = "www.example.com"
naked_domain = "example.com"
page_path = request.fullpath
if request.env['HTTP_HOST'] == naked_domain && RAILS_ENV == 'production'
#HTTP 301 is a permanent redirect
redirect_to "http://#{app_domain}#{page_path}", :status => 301
end
end
This works but I know I should be doing this in Rack Middleware.
Can someone show me how to set this up with the rack-rewrite gem?
I have added the following to my application_controller.rb file:
config.middleware.insert_before(Rack::Lock, Rack::Rewrite) do
r301 %r{.*}, 'http://www.example.com$&', :if => Proc.new {|rack_env|
rack_env['SERVER_NAME'] != 'www.example.com'
}
end
Which produces the following error:
NameError (undefined local variable or method `config' for ApplicationController:Class):
Any ideas on how I can get this setup working?