在 Heroku 上设置自定义域,并使用 www 子域的 CNAME 重定向

发布于 2024-09-05 22:43:36 字数 1549 浏览 5 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何以笙箫默 2024-09-12 22:43:36

CNAME 不是重定向,而只是您的域的规范名称。这意味着它的行为就像它指向的域(在您的例子中是 myapp.com )。您的浏览器获取与 myapp.com 相同的 IP 地址并向其发送请求。

重定向在 HTTP 级别或更高级别执行。例如,您可以在您的应用程序中执行此操作,或者为此创建另一个简单的应用程序。

下面是一个直接在您的应用中进行重定向的简单示例:

# 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

或者您可以使用 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

您还可以使用正则表达式来匹配所有带有 www. 的请求。在主机名前面。

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

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 could also use a Regex to match all requests with www. in front of the hostname.

只为一人 2024-09-12 22:43:36

这里有一个针对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')))
})

这也会将domain.herokuapp.com重定向到yourdomain.com,防止搜索引擎索引重复内容。

And here's a solution for 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.

谜兔 2024-09-12 22:43:36

您还可以在任何 Rack 应用程序中轻松完成此操作,只需安装 rack-canonical-host gem 并将其放入 Rack 配置文件中即可:

require 'rack-canonical-host'
use Rack::CanonicalHost, 'myapp.com'

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:

require 'rack-canonical-host'
use Rack::CanonicalHost, 'myapp.com'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文