首页 > 解决方案 > 无法将 http 请求重定向到 https

问题描述

您好,我想将我的 http 请求重定向到 https。我向 .htaccess 添加了一些代码,但我仍然可以使用 http 访问我的网站。出了什么问题,我该怎么办?

<IfModule mod_rewrite.c>

  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-l
  RewriteRule . /index.html [L]

</IfModule>
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteOptions inherit
Header set content-Security-Policy: upgrade-insecure-requests

标签: .htaccessredirectmod-rewriteurl-rewritingcpanel

解决方案


通过您显示的尝试,您能否尝试遵循 htaccess rues 设置。由于您对规则使用继承选项,因此如果您想在所有 URL 中应用 https,那么最好将您的 htpps 规则放在您的 BASE(非常一级文件夹明智)htaccess 文件中,如果您愿意,我们不需要将它们放在这里仅将 https 应用于此处介绍的 URL,然后使用以下样式规则。

请确保在测试您的 URL 之前清除您的浏览器缓存。

<IfModule mod_rewrite.c>

  RewriteOptions inherit
  RewriteEngine On
  RewriteCond %{HTTPS} off
  RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
  
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-l
  RewriteRule . /index.html [L]

</IfModule>

Header set content-Security-Policy: upgrade-insecure-requests

推荐阅读