首页 > 解决方案 > 无法获得正确的 URL 重定向到我的网站

问题描述

我在 NameCheap 上有自己的域名,我们称之为 example.com。我还有一个指向仅服务于 https 流量的 s3 存储桶的云端分发,我们称之为 myCloudfront.cloudfront.net

如果我去 myCloudfront.cloudfront.net 我被重定向到https://myCloudfront.cloudfront.net并且一切看起来都很好。

在 NameCheap 上,我有一条 CNAME 记录,其中包含以下值:

Type         Host           Value                TTL
CNAME        www  myCloudfront.cloudfront.net    Automatic

问题是我被重定向到我的云端分发的唯一方法是当我专门输入:https://www.example.com,如果我省略了 www。或尝试使用 http 它要么找不到该站点,要么因为它不是 https 而被拒绝。

如何设置 NameCheap 站点,以便如果用户键入以下任何内容,他们将被重定向到https://example.com

example.com
www.example.com
http://example.com
http://www.example.com
https://www.example.com

谢谢!

标签: amazon-web-servicesamazon-s3httpsdnsamazon-cloudfront

解决方案


嗯......在我看来,在 NameCheap 中,你可以这样做:

Host   Value               TTL
 @     your_server's_ip Automatic
www       @             Automatic

因此,example.com 和 www.example.com 都将指向 example.com 服务器。

在 example.com 上,安装 Apache(我假设 example.com 是一个 Linux 服务器?),并在 /etc/httpd/conf/httpd.conf 中设置一堆 VirtualHost,以处理 http -> 之间的转发和重定向https 和 https -> cdn 等。这样的事情可能会起作用:

<VirtualHost *:80>
    ServerAdmin webmaster@example.com
    ServerName example.com
    Redirect https://myCloudfront.cloudfront.net/
</VirtualHost>
<VirtualHost *:80>
    ServerAdmin webmaster@example.com
    ServerName www.example.com
    Redirect https://myCloudfront.cloudfront.net
</VirtualHost>
<VirtualHost *:443>
    ServerAdmin webmaster@example.com
    ServerName example.com
    Redirect https://myCloudfront.cloudfront.net/
</VirtualHost>
<VirtualHost *:443>
    ServerAdmin webmaster@example.com
    ServerName www.example.com
    Redirect https://myCloudfront.cloudfront.net
</VirtualHost>

如有必要,您可以通过转发和重定向获得更多创意。

上述四项规则应涵盖:

http://example.com
http://www.example.com
https://example.com
https://www.example.com

推荐阅读