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

时间:2018-06-05 21:05:20

标签: .htaccess ssl redirect https url-redirection

我在.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这样的单页,它不起作用

请帮我写一个正确的方法。

1 个答案:

答案 0 :(得分:0)

您可以使用以下规则:

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

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

相关问题