I'm using Heroku, and have added a couple custom domains for my app, i.e. myapp.com
and www.myapp.com
.
我正在使用Heroku,并为我的应用添加了几个自定义域名,即myapp.com和www.myapp.com。
My DNS at GoDaddy has three A records for '@' pointing to three separate Heroku IPs, and a CNAME for the 'www' subdomain that points to proxy.heroku.com
.
我在GoDaddy的DNS有三个A记录,'@'指向三个单独的Heroku IP,一个CNAME用于'www'子域,指向proxy.heroku.com。
What I want to do is redirect any traffic to www.myapp.com
to myapp.com
. I tried setting the CNAME to '@', but that still remains at the same domain. Is there a way I can force this redirect at the DNS level?
我想要做的是将任何流量重定向到www.myapp.com到myapp.com。我尝试将CNAME设置为“@”,但仍然保留在同一个域中。有没有办法可以在DNS级别强制进行此重定向?
3 个解决方案
#1
8
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.
CNAME不是重定向,只是您域名的规范名称。这意味着它的行为就像它指向的域(在你的情况下是myapp.com)。您的浏览器获得与myapp.com相同的IP地址,并向其发送请求。
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.
重定向在HTTP级别或更高级别执行。您可以在您的应用中执行此操作,或者为此创建另一个简单的应用。
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
Or you could use rails metal, which would do the same, but much faster:
或者你可以使用rails metal,它会做同样的事情,但要快得多:
# 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 could also use a Regex to match all requests with www.
in front of the hostname.
您还可以使用正则表达式将所有请求与www匹配。在主机名前面。
#2
1
And here's a solution for node.js
这是node.js的解决方案
$ heroku config:set APP_HOST=yourdomain.com
app.configure('production', function() {
// keep this relative to other middleware, e.g. after auth but before
// express.static()
app.get('*', function(req, res, next) {
if (req.headers.host != process.env.APP_HOST) {
res.redirect('http://' + process.env.APP_HOST + req.url, 301)
} else {
next()
}
})
app.use(express.static(path.join(application_root, 'static')))
})
This will also redirect domain.herokuapp.com to yourdomain.com, preventing search engines indexing duplicate content.
这也会将domain.herokuapp.com重定向到yourdomain.com,阻止搜索引擎索引重复内容。
#3
0
You can also do this pretty easily in any Rack app, by installing the rack-canonical-host
gem and putting this in your Rack config file:
您也可以在任何Rack应用程序中轻松完成此操作,方法是安装rack-canonical-host gem并将其放入Rack配置文件中:
require 'rack-canonical-host'
use Rack::CanonicalHost, 'myapp.com'
#1
8
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.
CNAME不是重定向,只是您域名的规范名称。这意味着它的行为就像它指向的域(在你的情况下是myapp.com)。您的浏览器获得与myapp.com相同的IP地址,并向其发送请求。
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.
重定向在HTTP级别或更高级别执行。您可以在您的应用中执行此操作,或者为此创建另一个简单的应用。
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
Or you could use rails metal, which would do the same, but much faster:
或者你可以使用rails metal,它会做同样的事情,但要快得多:
# 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 could also use a Regex to match all requests with www.
in front of the hostname.
您还可以使用正则表达式将所有请求与www匹配。在主机名前面。
#2
1
And here's a solution for node.js
这是node.js的解决方案
$ heroku config:set APP_HOST=yourdomain.com
app.configure('production', function() {
// keep this relative to other middleware, e.g. after auth but before
// express.static()
app.get('*', function(req, res, next) {
if (req.headers.host != process.env.APP_HOST) {
res.redirect('http://' + process.env.APP_HOST + req.url, 301)
} else {
next()
}
})
app.use(express.static(path.join(application_root, 'static')))
})
This will also redirect domain.herokuapp.com to yourdomain.com, preventing search engines indexing duplicate content.
这也会将domain.herokuapp.com重定向到yourdomain.com,阻止搜索引擎索引重复内容。
#3
0
You can also do this pretty easily in any Rack app, by installing the rack-canonical-host
gem and putting this in your Rack config file:
您也可以在任何Rack应用程序中轻松完成此操作,方法是安装rack-canonical-host gem并将其放入Rack配置文件中:
require 'rack-canonical-host'
use Rack::CanonicalHost, 'myapp.com'