CNAME is not a redirect, but a canonical name for your domain. This means that it behaves exactly the same as the domain it points to (myapp.com in your case). Your browser receives the same IP address as myapp.com and sends it a request.
Redirects run at the HTTP level or higher. You can do this, for example, in your application or create another simple application for this.
Here is a simple example for redirecting directly to your application:
# in your ApplicationController before_filter :strip_www def strip_www if request.env["HTTP_HOST"] == "www.myapp.com" redirect_to "http://myapp.com/" end end
Or you could use rails metal, which would do the same, but much faster:
# app/metal/hostname_redirector.rb class HostnameRedirector def self.call(env) if env["HTTP_HOST"] == "www.myapp.com" [301, {"Location" => "http://myapp.com/"}, ["Found"]] else [404, {"Content-Type" => "text/html"}, ["Not Found"]] end end end
You can also use Regex to match all requests from www. before the host name.
Tomas markauskas
source share