.htaccess 强制对某些页面和文件夹使用HTTPS,对所有其他页面和文件夹使用HTTP
我的.htaccess 强制对某些页面和文件夹使用HTTPS,对所有其他页面和文件夹使用HTTP,.htaccess,ssl,redirect,https,url-redirection,.htaccess,Ssl,Redirect,Https,Url Redirection,我的.htaccess中有,我想重定向example.com/login,example.com/profile和example.com/form.php 它只适用于文件夹,但不适用于页面 这是我的.htaccess Options +FollowSymLinks -MultiViews DirectoryIndex index.php RewriteEngine On # Force HTTP to HTTPS RewriteCond %{HTTPS} =off [NC] RewriteC
.htaccess
中有,我想重定向example.com/login
,example.com/profile
和example.com/form.php
它只适用于文件夹,但不适用于页面
这是我的.htaccess
Options +FollowSymLinks -MultiViews
DirectoryIndex index.php
RewriteEngine On
# Force HTTP to HTTPS
RewriteCond %{HTTPS} =off [NC]
RewriteCond %{THE_REQUEST} /(login|profile|form\.php) [NC]
RewriteRule ^(login|profile|form\.php) https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]
# Force HTTPS to HTTP
RewriteCond %{HTTPS} =on [NC]
RewriteCond %{THE_REQUEST} !/(login|profile|form\.php) [NC]
RewriteRule !^(login|profile|form\.php) http://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]
因此,如果我的用户进入http://example.com/login
,他被重定向到https://example.com/login
。配置文件页面也是如此
但是,对于像example.com/form.php
这样的单个页面,它不起作用
请帮我用正确的方式写 您可以使用以下规则:
Options +FollowSymLinks -MultiViews
DirectoryIndex index.php
RewriteEngine On
# Force HTTP to HTTPS
RewriteCond %{HTTPS} =off [NC]
RewriteCond %{THE_REQUEST} \s/+(login|profile|form\.php) [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
# Force HTTPS to HTTP
RewriteCond %{HTTPS} =on [NC]
RewriteCond %{THE_REQUEST} !\s/+(login|profile|form\.php) [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
# more rules go below this line
确保在浏览器中对此进行测试或完全清除浏览器缓存。为什么不在所有地方保留HTTPS?几个月后,浏览器将显示HTTP连接为“不安全”,那么为什么不正确切换呢?您获得了安全性,并且完全消除了问题。您可以通过编辑问题来显示您的完全.htaccess吗。