![](/img/trans.png)
[英]Htaccess Redirect: WWW + HTTPS://WWW + HTTP:// ---> HTTPS://non-www
[英]redirect www to non-www https with apache & .htaccess
我已经看到很多这样的问题,但是我还没有找到适合我的情况的问题。 我几乎尝试了其中的每一个,但无法使其正常工作。
有人可以解释一下如何在没有www的情况下使www.domain.com
和www.subdomain.domain.com
重定向到https
吗?
这是我到目前为止的内容:
目前,我有以下DNS记录:
A @ 1.2.3.4
A subdomain 1.2.3.4
CNAME www domain.com.
CNAME www.subdomain subdomain.domain.com.
我还有一个虚拟主机文件,如下所示(子域的副本几乎也一样) :
<VirtualHost *:80>
RewriteEngine on
ReWriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]
</VirtualHost>
<VirtualHost *:443>
ServerName domain.com
ServerAlias www.domain.com
ServerAdmin webmaster@localhost
DocumentRoot /path/to/public_html
SSLEngine on
SSLCertificateFile /path/to/domain.crt
SSLCertificateKeyFile /path/to/domain.key
SSLCertificateChainFile /path/to/domain-bundle
<Directory "/path/to/public_html">
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/domain-error.log
CustomLog ${APACHE_LOG_DIR}/domain-access.log combined
</VirtualHost>
在443虚拟主机中,尝试添加:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R]
这就是我所学到的。 www.domain.com
与domain.com
不同。 这也包括子域。 理想情况下,这两个名称都需要具有SSL认证。 但是,如果您像我一样,我真的不想花这笔钱。
如果用户在地址栏中键入以下内容,则可以进行以下修复:
http://www.domain.com => Will be redirected to https://domain.com
www.domain.com => Will be redirected to https://domain.com
https://domain.com => Will stay as is, secured with no errors.
https://www.domain.com => This is the only thing that will give a user a notice.
Luckily, I don't see any user typing this into the browser.
我已从DNS中删除了以下内容,因为我附带的条款是www.subdomain.domain.com
不切实际,除非我花更多的钱在另一个CERT上,但是如果他们使用一个www
。
CNAME www.subdomain subdomain.domain.com.
现在这是我的新VirtualHost *:80
,我用问题中的那个替换了。
<VirtualHost *:80>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]
</VirtualHost>
再次感谢乔恩·林(Jon Lin)的回复和帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.