.htaccess:根据 URL 将 http 重定向到 https 或 https 到 http
下面的解决方案涵盖了必需的规则:
1) http://www.mydomain.com 、 http://www.mydomain.com/?p=home 、 http://www.mydomain.com mydomain.com/?p=home1 、 http://www.mydomain.com/?qqq=home
始终为 http,即使键入 https 而不是 http;
2) 所有其余页面始终为 https,即使键入的是 http 而不是 https;
但实际上不涵盖
3) //www.mydomain.com/administration/any_file_in_admin_folder.php
也应始终为 https(即使使用参数 ?p=home 等) )。
RewriteEngine on
RewriteBase /
#determine if page is supposed to be http
#if it has p=home or p=home1 or qqq=home in querystring
RewriteCond %{QUERY_STRING} (^|&)(p=home1?|qqq=home)(&|$) [NC,OR]
#or if query string is empty
RewriteCond %{QUERY_STRING} ^$
#set env var to 1
RewriteRule ^ - [E=IS_HTTP:1]
#all pages that are supposed to be http redirected if https
RewriteCond %{HTTPS} on
RewriteCond %{ENV:IS_HTTP} 1
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#all other pages are sent to https if not already so
RewriteCond %{HTTPS} off
RewriteCond %{ENV:IS_HTTP} !1
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
如何实现 3) 这段代码才能正常工作? (我仍然没有运气,需要帮助)。 谢谢。
The solution below covers required rules:
1) http://www.mydomain.com , http://www.mydomain.com/?p=home , http://www.mydomain.com/?p=home1 , http://www.mydomain.com/?qqq=home
are always http, even if https are typed instead of http;
2) all the rest pages are always https, even if http was typed instead of https;
but in practice does NOT cover
3) //www.mydomain.com/administration/any_file_in_admin_folder.php
should always be https as well (even with parameter ?p=home etc).
RewriteEngine on
RewriteBase /
#determine if page is supposed to be http
#if it has p=home or p=home1 or qqq=home in querystring
RewriteCond %{QUERY_STRING} (^|&)(p=home1?|qqq=home)(&|$) [NC,OR]
#or if query string is empty
RewriteCond %{QUERY_STRING} ^$
#set env var to 1
RewriteRule ^ - [E=IS_HTTP:1]
#all pages that are supposed to be http redirected if https
RewriteCond %{HTTPS} on
RewriteCond %{ENV:IS_HTTP} 1
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#all other pages are sent to https if not already so
RewriteCond %{HTTPS} off
RewriteCond %{ENV:IS_HTTP} !1
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
How to implement 3) to this code so it works? (I still have no luck and need help).
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
在第二行和第二行中添加第一行第三块。
即
RewriteCond %{REQUEST_URI} !/administration/ [NC]
Try this:
Add the first line in 2nd & 3rd block.
i.e.
RewriteCond %{REQUEST_URI} !/administration/ [NC]
将您的第一条规则更改为:
Change your first rule to:
规则的第二部分
RewriteCond %{QUERY_STRING} ^$
匹配任何不带查询字符串的 URL。您的网址//www.mydomain.com/administration/any_file_in_admin_folder.php
没有查询字符串。因此,IS_HTTP
设置为 1,并且您的用户将被重定向到 HTTP。试试这个。它未经测试 - 但基本上您首先识别“home”查询字符串,然后单独处理
http://www.mydomain.com
。这也可能会起作用:
The second part of your rule,
RewriteCond %{QUERY_STRING} ^$
matches any URL without a querystring. Your URL,//www.mydomain.com/administration/any_file_in_admin_folder.php
has no querystring. SoIS_HTTP
is being set to 1, and your user is being redirected to HTTP.Try this. It's untested - but basically you're identifying the "home" querystring first, and then handling
http://www.mydomain.com
separately.This might also do the trick: