我在.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
这样的单页,它不起作用
请帮我写一个正确的方法。
答案 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
确保在浏览器中对此进行测试或完全清除浏览器缓存。