对某些页面和文件夹强制使用 HTTPS,对所有其他页面和文件夹强制使用 HTTP



我有在我的.htaccess,我想重定向example.com/loginexample.com/profileexample.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

确保在浏览器中对此进行测试或完全清除浏览器缓存。

最新更新