将HTTP重定向到HTTPS Apache2 [英] Redirect HTTP to HTTPS Apache2

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

问题描述

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

im trying to redirect http to https. I've found a lot answers, but nothing works for me. I dont know why, maybe its a apache2 config error? I tryin it also in the .htaccess and there also nothing happens.

只是这个错误:

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

Bad Request Your browser sent a request that this server could not understand. Reason: You're speaking plain HTTP to an SSL-enabled server port. Instead use the HTTPS scheme to access this URL, please.

这是我的虚拟主机文件。

Here's my Virtual Host File.

#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>


推荐答案

使用 VirtualHost *:80 *:443或相同的virtualhost标记中的相反内容是完全不正确的,因为一个虚拟主机不能同时是SSL和非SSL。

Using "VirtualHost *:80 *:443" or the opposite in the same virtualhost tag is completely incorrect since one virtualhost can't be SSL and not be SSL at the same time.

Apache HTTPD不会尖叫的事实痛苦的原因是,您可以在同一虚拟主机中使用不同的端口,但这肯定不是设计为同时具有SSL端口和非SSL端口。

The fact that Apache HTTPD is not screaming in pain about it is because you "can" use different ports in the same virtualhost but that was certainly not designed to have a SSL port and a non-SSL port together.

因此,我的建议是您将配置更正为合理,即分别具有特定的虚拟主机*:80和虚拟主机*:443。

So my suggestion is you correct your configuration to look sane, that is, having specific virtualhost *:80 and virtualhost *:443 separately.

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

In the VirtualHost *:80 entries you can then Redirect / https://example.com/ with the specific hostnames for your case to redirect from 80 to 443 a single line and without the need to use mod_rewrite.

不需要重定向到SSL,mod_rewrite不需要过多使用。

To redirect to SSL mod_rewrite is not needed and overkill.

简而言之:

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

其余名称如果配置不同,则保持不变。

And the same with the rest of the names if they have different configurations.

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

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