Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache 我怎么能强迫你呢https://www. 要重定向到非www https url的我的域的版本?_Apache_.htaccess_Mod Rewrite_Redirect - Fatal编程技术网

Apache 我怎么能强迫你呢https://www. 要重定向到非www https url的我的域的版本?

Apache 我怎么能强迫你呢https://www. 要重定向到非www https url的我的域的版本?,apache,.htaccess,mod-rewrite,redirect,Apache,.htaccess,Mod Rewrite,Redirect,我正在尝试让我的域始终重定向到非www版本,并始终使用https。我发现了,但它只起作用 如果我尝试以下任何一种方法,它都会起作用: http://domain.com (This will redirect properly to https://domain.com) http://www.domain.com (This will redirect properly to https://domain.com) https://domain.com (This does nothing a

我正在尝试让我的域始终重定向到非www版本,并始终使用https。我发现了,但它只起作用

如果我尝试以下任何一种方法,它都会起作用:

http://domain.com (This will redirect properly to https://domain.com)
http://www.domain.com (This will redirect properly to https://domain.com)
https://domain.com (This does nothing as it is already correct)
但是,如果我键入
https://www.domain.com
然后我得到一个错误,即域不安全,并且没有重定向

以下是我的.htaccess规则:

############ START [ENABLE REWRITE ENGINE] ############
RewriteEngine On
RewriteBase /
############  END [ENABLE REWRITE ENGINE]  ############

############ START [FORCE NON-WWW AND HTTPS] ############
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule .* https://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule .* https://%1%{REQUEST_URI} [R=301,L]
############  END [FORCE NON-WWW AND HTTPS]  ############

我知道一种选择是只为该域的www.版本购买SSL,但如果可能的话,我更愿意省钱。

由于HTTPS协商发生在发出任何请求之前,您无法在证书名称不匹配时重定向到正确的主机名。您可以获得便宜的SSL证书,这些证书将www和域作为主题替代名称

大多数ssl证书(即使是$5正数/rapidssl证书)都适用于域名和www.domainame(它们将其作为主题替代名称)。但是,由于它不安全而导致的错误是由于证书失败?如果您的证书没有覆盖,将无法重定向它,因为https协商是在连接时进行的。在提出任何请求之前/重定向可以发生好吧,这不是我所希望的答案,但谢谢你!如果你把这当作一个答案,我会接受的。每当你购买证书时,经验法则是为子域购买,
www.domain.com
,那么你会自动从大多数CA获得
domain.com
。然而,仅仅做
domain.com
并不总是给你
www.domain.com
。那个不这么做的人只是贪心,想让你买2个。不要使用它们
ssls.com
对于普通证书来说非常便宜。