.htaccess 从 http 重定向到 https

.htaccess redirecting from http to https

以前可能有人问过这个问题,但我找不到答案。

我最近购买了 SSL 证书,我需要帮助设置 .htaccess 文件。

我使用的是 MVC 设计模式,所以每一页都会通过 index.php

这是我当前的 .htaccess 配置,所有页面都通过 https

RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ index.php?url= [QSA,L]

我了解到在整个网站上使用 https 会增加开销。是否可以为特定页面添加规则,例如将重定向到 https 而不是 http 的 login/register/forgot 密码?

将特定页面重定向到 https:

RewriteEngine On
RewriteBase /

# specific pages to https
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} /(login|register|forgotPassword) [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# all other pages to http
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} !/(login|register|forgotPassword) [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url= [QSA,L]