ani*_*son 5 mod-rewrite mod-ssl .htaccess apache-2.2
我以前从未遇到过这样的事情。如果确定 HTTPS 已关闭,我试图简单地将页面重定向到 HTTPS 版本,但它显示的是 HTML 页面而不是实际重定向;更奇怪的是,它将它显示为文本/纯文本!
VirtualHost 声明(排序):
ServerAdmin admin@example.com
DocumentRoot "/path/to/files"
ServerName example.com
SSLEngine On
SSLCertificateFile /etc/ssh/certify/example.com.crt
SSLCertificateKeyFile /etc/ssh/certify/example.com.key
SSLCertificateChainFile /etc/ssh/certify/sub.class1.server.ca.pem
<Directory "/path/to/files/">
AllowOverride All
Options +FollowSymLinks
DirectoryIndex index.php
Order allow,deny
Allow from all
</Directory>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://example.com:6161 [R=301]
Run Code Online (Sandbox Code Playgroud)
页面输出:
ServerAdmin admin@example.com
DocumentRoot "/path/to/files"
ServerName example.com
SSLEngine On
SSLCertificateFile /etc/ssh/certify/example.com.crt
SSLCertificateKeyFile /etc/ssh/certify/example.com.key
SSLCertificateChainFile /etc/ssh/certify/sub.class1.server.ca.pem
<Directory "/path/to/files/">
AllowOverride All
Options +FollowSymLinks
DirectoryIndex index.php
Order allow,deny
Allow from all
</Directory>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://example.com:6161 [R=301]
Run Code Online (Sandbox Code Playgroud)
我试过将重写内容移到 SSL 内容之上,希望它能做些什么而什么也没有发生。如果我通过 HTTPS 查看页面,它会显示正常。显然检测到我正在尝试重写路径,但它并没有起作用。
Apache 错误日志没有向我表明任何可能出错的地方。
当我删除 RewriteRules 时:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="https://example.com:6161">here</a>.</p>
<hr>
<address>Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/1.0.0e DAV/2 Server at example.com Port 443</address>
</body></html>
Run Code Online (Sandbox Code Playgroud)
我得到标准的“你不能这样做,因为你没有使用 SSL”响应,它也以文本/纯文本形式提供,而不是呈现为 HTML。这是有道理的,它应该只适用于启用 HTTPS 的连接,但是当它确定未启用时,我仍然想将它们重定向到 HTTPS 连接。
认为我可以绕过系统:
我尝试ErrorDocument 400 https://example.com:6161在配置文件中添加 a而不是使用 RewriteRules,这只是给了我一条新消息,仍然没有奶酪。
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
Reason: You're speaking plain HTTP to an SSL-enabled server port.<br />
Instead use the HTTPS scheme to access this URL, please.<br />
<blockquote>Hint: <a href="https://example.com/"><b>https://example.com/</b></a></blockquote></p>
<p>Additionally, a 404 Not Found
error was encountered while trying to use an ErrorDocument to handle the request.</p>
<hr>
<address>Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/1.0.0e DAV/2 Server at example.com Port 443</address>
</body></html>
Run Code Online (Sandbox Code Playgroud)
如何强制 Apache 实际重定向而不是显示以纯文本格式显示 HTML 的“301”页面?
小智 1
第一:使用 Apache,您不能在同一端口上运行 HTTPS 和 HTTP。
请参阅:Apache 在同一端口上同时应答 HTTP 和 HTTPS
为了处理 SSL 和非 SSL 流量,您必须定义两个虚拟主机。
请参阅:在不同端口上将 http 重定向到 https apache
对于从非 SSL 虚拟主机到 SSL 虚拟主机的重定向,我们使用以下命令:
<VirtualHost *:80>
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(.*)$
RewriteRule ^(.*)/? https://%1$1 [L,R]
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)
仍然存在的问题是,显式发送非 SSL 请求到启用 SSL 的端口 ( http://mydomain.com:443/ ) 会导致:
You're speaking plain HTTP to an SSL-enabled server port
Run Code Online (Sandbox Code Playgroud)
纯文本页面(在 Firefox 浏览器中)。向非 SSL 端口 ( https://mydomain.com:80/ ) 显式发送 SSL 请求会得到:
ssl_error_rx_record_too_long error
Run Code Online (Sandbox Code Playgroud)
(在火狐浏览器中)。
为什么会出现这种情况?
| 归档时间: |
|
| 查看次数: |
4622 次 |
| 最近记录: |