简体   繁体   English

重定向Heroku HTTPs://域

[英]Redirecting Heroku HTTPs:// Domain

To improve the SEO on my site I'd like the different variations of my domain to link to one main site. 为了改善我网站上的SEO,我希望将我的域的不同版本链接到一个主要网站。

For example right now https://example.com and https://www.example.com both access the same domain and are viewed as different websites by Google. 例如,现在https://example.comhttps://www.example.com都访问相同的域,并且被Google视为不同的网站。

I'm hosting my Rails app on Heroku and then connecting to the domain I purchased through DNSimple. 我将Hers应用程序托管在Heroku上,然后连接到通过DNSimple购买的域。 Does anyone know how to get all of these websites to redirect to https://www.example.com ? 有谁知道如何让所有这些网站重定向到https://www.example.com

I've followed different tutorials on Heroku (adding domains) and DNSimple (adding an ALIAS), but nothing has worked. 我遵循了有关Heroku(添加域)和DNSimple(添加ALIAS)的不同教程,但是没有任何效果。

Keeping your site hosted on two different domain does not mean improved SEO, in facts it duplicate content for Google and is not appreciated. 将您的网站托管在两个不同的域上并不意味着SEO会得到改善,实际上,它可以为Google复制内容,因此不胜感激。

To improve your SEO you should be doing a 301 redirect to the domain which you want to be shared as primary domain. 为了改善您的SEO,您应该执行301 redirect到要共享为主域的域。

About setting your preferred domain (www or non-www) . 关于设置首选域(www或非www)

On setting up redirect - Heroku how to redirect http:// to http://www 关于设置重定向-Heroku如何将http://重定向到http:// www

There are a few complications here. 这里有一些并发症。

The first complication is that you want to redirect an hostname which is using HTTPs. 第一个麻烦是您要重定向使用HTTP的主机名。 In fact, normally you could use the DNSimple URL record, but it doesn't work with https as you would need to install the SSL certificate on DNSimple servers (HTTP redirect header is sent after the HTTPS negotiation). 实际上,通常您可以使用DNSimple URL记录,但是它不适用于https,因为您需要在DNSimple服务器上安装SSL证书(HTTP重定向头在HTTPS协商后发送)。

One option would be to redirect at server level, but since your app is hosted on Heroku you have no control over the server configuration. 一种选择是在服务器级别重定向,但是由于您的应用程序托管在Heroku上,因此您无法控制服务器配置。

Therefore, in your case the only option is to point the various domains to the same Heroku application, add the domains as custom domains to your Heroku app and handle the redirect internally. 因此,在您的情况下,唯一的选择是将各个域指向同一个Heroku应用程序,将这些域作为自定义域添加到您的Heroku应用程序中,并在内部处理重定向。 That means your app should check whether a request comes from one of the alternate domain and if that's the case it redirects to the canonical domain. 这意味着您的应用程序应检查请求是否来自备用域之一,如果是这种情况,它将重定向到规范域。

There are several ways to check it in a Rails app. 有几种方法可以在Rails应用程序中进行检查。 The most effective is probably using a custom middleware or the Rails router file, so that the request is handled as soon as possible and it doesn't build an entire request stack in the app, as it would happen for instance with a before_action . 最有效的方法可能是使用自定义的中间件或Rails路由器文件,以便尽快处理请求,并且不会在应用程序中构建整个请求堆栈,例如使用before_action

On the DNSimple side, you will just have to create an ALIAS record (if the hostname you want to resolve is an apex domain) or a CNAME record (if the hostname is a subdomain) that points to the same Heroku SSL endpoint of your application. 在DNSimple方面,您只需要创建一个ALIAS记录(如果要解析的主机名是一个顶点域)或一个CNAME记录(如果主机名是一个子域),指向您的应用程序的相同Heroku SSL端点。

However, the last complication is that Heroku doesn't allow multiple certificates for the same app. 但是,最后一个麻烦是Heroku不允许同一应用程序使用多个证书。 If the domains you want to redirect to the canonical one belongs to the same name (eg are subdomains) you can just purchase a wildcard certificate. 如果要重定向到规范域的域属于同一名称(例如,子域),则可以购买通配符证书。 Otherwise, if you want to cover multiple different domains in the redirect, you will need a multi-domain SSL certificate. 否则,如果要在重定向中包含多个不同的域,则将需要一个多域SSL证书。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM