开发者

Setting up custom domain on Heroku with CNAME redirect for www subdomain [closed]

开发者 https://www.devze.com 2023-01-03 20:17 出处:网络
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.

Closed 6 years ago.

开发者_开发技巧 Improve this question

I'm using Heroku, and have added a couple custom domains for my app, i.e. myapp.com and 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.

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?


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.


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.


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'
0

精彩评论

暂无评论...
验证码 换一张
取 消