当在浏览器的 URL 栏中输入 localhost 时,它会被正确重定向到https://localhost
,但我的代码中所有用于加载文件的调用http
都没有被重定向或重写到https
。例如:
<link rel="stylesheet" type="text/css" href="http://static/css/colwidth.min.css">
我已经尝试Redirect permanent / https://localhost/
在 Apache 配置文件中VirtualHost
,并且还尝试RewriteRule
过.htaccess
Firefox
我收到的错误是:
阻止加载混合活动内容“
http://static/css/colwidth.min.css
”
我将非常感激帮助了解如何做到这一点两个都Apache 配置和.htaccess
(我知道.htaccess
这不是首选方法 - 但我仍然想了解为什么它不起作用)
网址:
<VirtualHost *:80>
ServerAdmin me@localhost
DocumentRoot "D:/Website/path/to/root"
ServerName localhost
Redirect permanent / https://localhost/
<Directory "D:/Website/path/to/root">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require ip 127.0.0.1
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerAdmin me@localhost
DocumentRoot "D:/Website/path/to/root"
ServerName static
Redirect permanent / https://static/
<Directory "D:/Website/path/to/root">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require ip 127.0.0.1
</Directory>
</VirtualHost>
<VirtualHost *:443>
DocumentRoot "D:/Website/path/to/root"
ServerName localhost:443
ServerAdmin me@localhost
ErrorLog "c:/xampp/apache/logs/error.log"
TransferLog "c:/xampp/apache/logs/access.log"
<Directory "D:/Website/path/to/root">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require ip 127.0.0.1
</Directory>
SSLEngine on
SSLCertificateFile "c:/xampp/apache/bin/wtr.cert"
SSLCertificateKeyFile "c:/xampp/apache/bin/wtr.key"
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory "c:/xampp/apache/cgi-bin">
SSLOptions +StdEnvVars
</Directory>
CustomLog "c:/xampp/apache/logs/ssl_request.log" \
"%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
</VirtualHost>
https:
<VirtualHost *:443>
DocumentRoot "D:/Website/path/to/root"
ServerName static:443
ServerAdmin me@localhost
ErrorLog "c:/xampp/apache/logs/error.log"
TransferLog "c:/xampp/apache/logs/access.log"
<Directory "D:/Website/path/to/root">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require ip 127.0.0.1
</Directory>
SSLEngine on
SSLCertificateFile "c:/xampp/apache/bin/static.cert"
SSLCertificateKeyFile "c:/xampp/apache/bin/static.key"
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory "c:/xampp/apache/cgi-bin">
SSLOptions +StdEnvVars
</Directory>
CustomLog "c:/xampp/apache/logs/ssl_request.log" \
"%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
</VirtualHost>
.htaccess
RewriteEngine On
# For SSL
RewriteCond %{HTTPS} !=on
RewriteRule (.*) https://%{SERVER_NAME}/$1 [L,R=301]
# I also tried:
#RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}[L,R=301]
#Rules for Versioned Static Files
RewriteRule ^(js|js-common|css|css-common|img|img-common)/(.+)\.([0-9])+\.(js|css|php|jpg|gif|png)(.*)$ $1/$2.$4$5 [L]
#rename invalid file and directory requests
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?redirectroot=true
答案1
这Firefox 错误我得到的是:
这就是问题所在……浏览器触发此错误前该请求甚至到达您的服务器,因此在服务器上进行任何重定向的尝试都为时已晚。
如果页面本身被重定向到,https://...
则该页面内引用的所有资源也必须使用https://...
,否则您会收到上述“混合内容”警告。这是基本的浏览器安全性,以防止任何安全的内容被泄露不安全(HTTP)连接。
不要使用绝对 URL(包括方案) 在 HTML 中你可以使用协议相对 URL 来代替,例如:
<link rel="stylesheet" type="text/css" href="//static/css/colwidth.min.css">