【问题标题】:Force SSL not working with .htaccess强制 SSL 不适用于 .htaccess
【发布时间】:2018-05-21 23:02:35
【问题描述】:

我已经在我的 .htaccess 文件中尝试了以下所有方法。

#RewriteEngine On
#RewriteCond %{HTTPS} off
#RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

#RewriteEngine On
#RewriteCond %{SERVER_PORT} !443
#RewriteRule ^(/(.*))?$ https://%{HTTP_HOST}/$1 [R=301,L]

#RewriteEngine On
#RewriteCond %{HTTPS} off
#RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

RewriteEngine On

# If we receive a forwarded http request from a proxy...
RewriteCond %{HTTP:X-Forwarded-Proto} =http [OR]

# ...or just a plain old http request directly from the client
RewriteCond %{HTTP:X-Forwarded-Proto} =""
RewriteCond %{HTTPS} !=on

# Redirect to https version
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

我在这台服务器上有两个虚拟主机,但似乎都没有工作我在 httpd.conf 文件中启用了 mod-rewrite。有什么想法吗?

【问题讨论】:

  • 如果可以,请将您的配置放在 Apache 的主文件(或其中包含的一个)中,而不是 .htaccess。您将获得更高的安全性、更简单和更好的性能。
  • 也许您的主机运行 apache 和 nginx,因此您的 .htaccess 被某些文件绕过?

标签: apache .htaccess ssl


【解决方案1】:

不推荐使用mod_rewrite 将HTTP 重定向到HTTPS。你应该在你的虚拟主机配置中使用RedirectSSL

例子:

<VirtualHost *:80>
   ServerName example.com
   Redirect / https://example.com/
</VirtualHost>

<VirtualHost _default_:443>
    ServerName example.com

    # server settings e.g ProxyPass or DocumentRoot

    SSLEngine On
    # other ssl settings
</VirtualHost>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-17
    • 2014-03-31
    • 2013-04-28
    • 2012-02-02
    • 2012-04-22
    • 2017-08-22
    • 2023-03-27
    • 2018-04-22
    相关资源
    最近更新 更多