.htaccess - 在 .htaccess 中强制使用 SSL 和 WWW

标签 .htaccess mod-rewrite ssl web

我四处寻找强制 SSL 和 www 的方法。但我发现的许多解决方案都没有奏效。目前我正在尝试这个:

RewriteCond %{HTTP_HOST} ^domain\.co\.uk$ [OR]
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://www.domain.co.uk/$1 [R,L]
  • domain.co.uk -> https://www.domain.co.uk
  • www.domain.co.uk -> https://domain.co.uk
  • https://domain.co.uk -> https://domain.co.uk 存在 SSL 错误

(空格是因为我太新,无法发布超过 2 个链接)。

有没有办法做到这一点?在尝试了至少 10 个不同的示例之后,令我惊讶的是它们最终都在最后一个上失败了。

最佳答案

您的问题是您指定了 HTTP 主机但在您的条件中缺少 HTTPS 主机。像这样:

RewriteCond %{HTTP_HOST} ^domain\.co\.uk$ [OR]
RewriteCond %{HTTPS_HOST} ^domain\.co\.uk$ [OR]
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://www.domain.co.uk/$1 [R,L]  

应该这样做。

关于.htaccess - 在 .htaccess 中强制使用 SSL 和 WWW,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14693739/

相关文章:

javascript - Steam 发布请求 - 无法加载资源 : the server responded with a status of 400 (Bad Request)

php - 如何在 .htaccess 重定向后通过 ajax 请求保留 POST 数据?

python - 配置 htaccess 以提供静态 django 文件

php - .htaccess seo 重写

.htaccess - htaccess : redirect all requests to different domain (without query arguments)

mod-rewrite - 将 URL 从 .json 重写为 .php

php - CURL 请求使 SSL 崩溃

python - WordPress Api 请求错误 : [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl. c:700)

http - 默认通过https访问nginx上的站点

.htaccess - 亚马逊弹性 beantalk : Use nginx/apache to forward subdomains to subfolders