CNAME is not a redirect but only a canonical name for your domain. That means that it behaves just like the domain it points to (myapp.com in your case). Your browser gets the same IP address as myapp.com has and sends a request to it.
Redirects are performed at the HTTP level or above. You can do this for example in your app or create another simple app just for that.
Here's a simple example to do the redirect directly in your app:
# 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
または、レール メタルを使用することもできます。これは同じことを行いますが、はるかに高速です。
# 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
www.
正規表現を使用して、ホスト名の前にあるすべてのリクエストを照合することもできます。