Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.htaccess 强制对某些页面和文件夹使用HTTPS,对所有其他页面和文件夹使用HTTP_.htaccess_Ssl_Redirect_Https_Url Redirection - Fatal编程技术网

.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吗。