将 https 重定向到非 www,将 http 重定向到 www [英] Redirect https to non-www and http to www

查看:60
本文介绍了将 https 重定向到非 www,将 http 重定向到 www的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要结合重定向来实现以下目标:

I need a combination of redirects to achieve the following:

http://example.com 重定向到 http://www.example.com,同时重定向 https://www.example.comhttps://example.com.

To redirect http://example.com to http://www.example.com, while redirecting https://www.example.com to https://example.com.

当通过 http 访问网站时,我想强制 www 前缀做域名.

I would like to force the www prefix do the domain name when the site is accessed over http.

但是 SSL 证书只能在没有 www 的情况下工作.

However the SSL certificate only works without the www.

当通过 https 访问时,我不希望域名带有 www 前缀.

When accessed over https, I don't want the domain name to have the www prefix.

推荐答案

https://www.mysite.comhttps://mysite.com 的重定向> 只能在客户端向 https://www.mysite.com 发出初始请求后发生.

The redirection from https://www.mysite.com to https://mysite.com can only happen after the client has made an initial request to https://www.mysite.com.

要使此初始连接正常工作,https://www.mysite.com 上的服务器必须具有对 www.mysite.com 有效的证书,否则,这种连接甚至不会发生(并且服务器不会发送重定向响应).

For this initial connection to work, the server at https://www.mysite.com must have a certificate valid for www.mysite.com, otherwise, this connection won't even happen (and the server won't send a redirection response).

如果您仍需要重定向,则在同一台服务器上,您的服务器必须提供对您要提供服务的主机名有效的证书.您应该获得一个包含两个主题备用名称 DNS 条目的证书:mysite.comwww.mysite.com;这将允许您使用相同的证书为两台主机提供服务(然后在需要时使用重写规则).

If you still want a redirection, on the same server, your server must present a certificate that is valid for the host names you want to serve. You should get a certificate with two Subject Alternative Name DNS entries: mysite.com and www.mysite.com; this will allow you to serve both hosts with the same certificate (and then use the rewrite rules if needed).

(如果您希望客户端支持,您也可以将服务器名称指示与两个不同的证书一起使用,但这通常用于完全不同的主机名.)

(You could also use Server Name Indication with two distinct certificates, if you expect the clients to support it, but that's usually for completely different host names.)

当您申请另一个时,CA 颁发对 mysite.comwww.mysite.com 都有效的证书是很常见的,有时没有额外收费.

It's quite common for CAs to issue certificates that are valid for both mysite.com and www.mysite.com when you apply for one of the other, sometimes without an extra fee.

这篇关于将 https 重定向到非 www,将 http 重定向到 www的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆