【问题标题】:Redirect HTTP to HTTPS Apache2将 HTTP 重定向到 HTTPS Apache2
【发布时间】:2016-12-09 02:25:37
【问题描述】:

我正在尝试将 http 重定向到 https。 我找到了很多答案,但没有什么对我有用。 我不知道为什么,可能是 apache2 配置错误? 我也在 .htaccess 中尝试过,也没有任何反应。

只是这个错误:

错误请求 您的浏览器发送了此服务器无法理解的请求。 原因:您对启用 SSL 的服务器端口使用纯 HTTP。 请改用 HTTPS 方案访问此 URL。

这是我的虚拟主机文件。

#Redirect HTTP TO HTTPS

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{SERVER_NAME}/%$1 [R,L]

#VHOSTS


<VirtualHost *:443>
    Servername www.latoya.eu
    ServerAlias latoya.eu www.latoya.eu
    Documentroot /var/www/latoya
    ErrorLog /path/to/log/error.log
    CustomLog /path/to/log/access.log combined
    SSLEngine on
    SSLCertificateFile /path/to/ssl/files/pem.crt
    SSLCertificateKeyFile /path/to/ssl/files/private.key
    SSLCertificateChainFile /path/to/ssl/files/pem.ca-bundle
</VirtualHost>

<VirtualHost *:443>
    Servername board.latoya.eu
    Documentroot /var/www/latoya
    ErrorLog /path/to/log/error.log
    CustomLog /path/to/log/access.log combined
    SSLEngine on
    SSLCertificateFile /path/to/ssl/files/pem.crt
    SSLCertificateKeyFile /path/to/ssl/files/private.key
    SSLCertificateChainFile /path/to/ssl/files/pem.ca-bundle
</VirtualHost>

<VirtualHost *:443 *:80>
    Servername secure.latoya.eu
    Documentroot /var/www/latoya
    ErrorLog /path/to/log/error.log
    CustomLog /path/to/log/access.log combined
    SSLEngine on
    SSLCertificateFile /path/to/ssl/files/pem.crt
    SSLCertificateKeyFile /path/to/ssl/files/private.key
    SSLCertificateChainFile /path/to/ssl/files/pem.ca-bundle
</VirtualHost>

<VirtualHost *:80 *:443>
    Servername static.kritzelpixel.com
    Documentroot /var/www/static.kritzelpixel.com
    ErrorLog /path/to/log/error.log
    CustomLog /path/to/log/access.log combined
    SSLCertificateFile /path/to/ssl/files/pem.crt
    SSLCertificateKeyFile /path/to/ssl/files/private.key
    SSLCertificateChainFile /path/to/ssl/files/pem.ca-bundle
</VirtualHost>

【问题讨论】:

  • 你需要用SSLEngine off设置80端口,用SSLEngine on设置443端口
  • 谢谢...!解决了我的问题

标签: apache .htaccess


【解决方案1】:

在同一个虚拟主机标签中使用“VirtualHost *:80 *:443”或相反的词是完全不正确的,因为一个虚拟主机不能同时是 SSL。

Apache HTTPD 并没有为此痛苦尖叫的事实是因为您“可以”在同一个虚拟主机中使用不同的端口,但这肯定不是为了同时拥有 SSL 端口和非 SSL 端口而设计的。

所以我的建议是您更正您的配置以使其看起来正常,即分别具有特定的虚拟主机 *:80 和虚拟主机 *:443。

然后,在 VirtualHost *:80 条目中,您可以使用特定主机名重定向/https://example.com/,将单行从 80 重定向到 443,而无需使用 mod_rewrite。

不需要重定向到 SSL mod_rewrite 并且矫枉过正。

简单地说:

<VirtualHost *:80>
ServerName example.com
Redirect / https://example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
SSLEngine on
#other directives here
</VirtualHost>

如果其他名称具有不同的配置,则与其余名称相同。

【讨论】:

  • *:80 和 *:443 都可以是 SSL 或非 SSL,但按照惯例,端口 80 应为非 SSL。
  • 语法允许它不会阻止它成为一个不应该用于相干抖动的完全异常。最后,一个虚拟主机将是 SSL 或不是 SSL,但不是两者兼而有之。
猜你喜欢
  • 2020-04-16
  • 1970-01-01
  • 2023-03-07
  • 2015-08-11
  • 2018-02-10
  • 1970-01-01
  • 2019-10-26
  • 2017-03-14
  • 2017-11-13
相关资源
最近更新 更多