.htaccess:根据URL将http重定向到https或https重定向到http

.htaccess:根据URL将http重定向到https或https重定向到http,.htaccess,mod-rewrite,https,flags,.htaccess,Mod Rewrite,Https,Flags,下面的解决方案涵盖了所需的规则: 1) http://www.mydomain.com , http://www.mydomain.com/?p=home , http://www.mydomain.com/?p=home1 , http://www.mydomain.com/?qqq=home始终是http,即使键入的是https而不是http 2) 所有剩余页面始终为https,即使键入的是http而不是https 但实际上不包括 3) //www.mydomain.com/administ

下面的解决方案涵盖了所需的规则:

1)
http://www.mydomain.com , http://www.mydomain.com/?p=home , http://www.mydomain.com/?p=home1 , http://www.mydomain.com/?qqq=home
始终是http,即使键入的是https而不是http

2) 所有剩余页面始终为https,即使键入的是http而不是https

但实际上不包括

3)
//www.mydomain.com/administration/admin\u文件夹中的任何\u文件\u.php
也应始终为https(即使使用参数?p=home等)

如何对该代码实施3)以使其正常工作?(我仍然没有运气,需要帮助)。
谢谢。

将第一条规则更改为:

#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} ^$
#or if request URI is: /administration/any_file_in_admin_folder\.php
RewriteCond %{REQUEST_URI} !^/*administration/any_file_in_admin_folder\.php$ [NC]
#set env var to 1
RewriteRule ^ - [E=IS_HTTP:1]

将第一条规则更改为:

#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} ^$
#or if request URI is: /administration/any_file_in_admin_folder\.php
RewriteCond %{REQUEST_URI} !^/*administration/any_file_in_admin_folder\.php$ [NC]
#set env var to 1
RewriteRule ^ - [E=IS_HTTP:1]

规则的第二部分,
RewriteCond%{QUERY\u STRING}^$
匹配任何不带querystring的URL。您的URL,
//www.mydomain.com/administration/any\u file\u in\u admin\u folder.php
没有查询字符串。因此
IS\u HTTP
被设置为1,您的用户被重定向到HTTP

试试这个。这是未经测试的-但基本上您首先要识别“主”查询字符串,然后处理
http://www.mydomain.com
分别列出

#if it has p=home or p=home1 or qqq=home in querystring
RewriteCond %{QUERY_STRING} (^|&)(p=home1?|qqq=home)(&|$) [NC]
#set env var to 1
RewriteRule ^ - [E=IS_HTTP:1]

RewriteRule ^$ - [E=IS_HTTP:1]
这也可能起到作用:

#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 %{REQUEST_URI} ^$
#set env var to 1
RewriteRule ^ - [E=IS_HTTP:1]

规则的第二部分,
RewriteCond%{QUERY\u STRING}^$
匹配任何不带querystring的URL。您的URL,
//www.mydomain.com/administration/any\u file\u in\u admin\u folder.php
没有查询字符串。因此
IS\u HTTP
被设置为1,您的用户被重定向到HTTP

试试这个。这是未经测试的-但基本上您首先要识别“主”查询字符串,然后处理
http://www.mydomain.com
分别列出

#if it has p=home or p=home1 or qqq=home in querystring
RewriteCond %{QUERY_STRING} (^|&)(p=home1?|qqq=home)(&|$) [NC]
#set env var to 1
RewriteRule ^ - [E=IS_HTTP:1]

RewriteRule ^$ - [E=IS_HTTP:1]
这也可能起到作用:

#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 %{REQUEST_URI} ^$
#set env var to 1
RewriteRule ^ - [E=IS_HTTP:1]
试试这个: 在第二块和第三块中添加第一行

i、 e.
RewriteCond%{REQUEST_URI}/管理/[NC]

RewriteEngine on
RewriteBase /

#determine if page is supposed to be http
#Requested URi must not have administration in it
RewriteCond %{REQUEST_URI} !/administration/ [NC]
#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]  [L]

#all pages that are supposed to be http redirected if https
RewriteCond %{REQUEST_URI} !/administration/ [NC]
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]
试试这个: 在第二块和第三块中添加第一行

i、 e.
RewriteCond%{REQUEST_URI}/管理/[NC]

RewriteEngine on
RewriteBase /

#determine if page is supposed to be http
#Requested URi must not have administration in it
RewriteCond %{REQUEST_URI} !/administration/ [NC]
#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]  [L]

#all pages that are supposed to be http redirected if https
RewriteCond %{REQUEST_URI} !/administration/ [NC]
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]

对不起,它坏了。administration文件夹中的任何文件都会转到http(也可以从https)。很抱歉,我之前犯了一个错误,只是修复了它。请试试。对不起,它不起作用。administration文件夹中的任何文件都会转到http(也可以从https)。很抱歉,我之前犯了一个错误,只是修复了它。请试试,谢谢。它是有效的(你的答案是我第一次尝试的)。有人能猜到它为什么会起作用,但一旦我创建了密码(我尝试了两种方法-使用主机面板设置->通过密码保护文件夹,以及创建.htaccess和.htpasswd文件),页面管理/index.php就会重定向到404错误页面。IE说(如果我输入了错误的密码):此外,在尝试使用ErrorDocument处理请求时遇到了404 Not Found错误。如果我输入正确,404错误页面显示没有错误。发生了什么事?没有密码-everythigsOK。@Haradzieniec您为
401错误
定义了一个单独的文档了吗?@Haradzieniec之所以有效,是因为,
RewriteCond%{REQUEST\u URI}/administration/[NC]
添加到第2和第3块(直接指向http
协议),检查URI中是否没有
administration
。我理解更新代码的工作原理(非常感谢)。我刚刚创建了401错误页面。是的,它转到401错误页,而不是404。我仍然不明白为什么它会重定向到401错误页面?另外,如果我键入(http),它会首先询问密码,然后在输入正确的密码后显示401.shtml。但是,如果我键入(现在使用httpS),它将显示,而不询问密码(每次在新浏览器中测试,以确保多次)。@Haradzieniec请确保您已正确设置
.htpasswd
身份验证。谢谢。它是有效的(你的答案是我第一次尝试的)。有人能猜到它为什么会起作用,但一旦我创建了密码(我尝试了两种方法-使用主机面板设置->通过密码保护文件夹,以及创建.htaccess和.htpasswd文件),页面管理/index.php就会重定向到404错误页面。IE说(如果我输入了错误的密码):此外,在尝试使用ErrorDocument处理请求时遇到了404 Not Found错误。如果我输入正确,404错误页面显示没有错误。发生了什么事?没有密码-everythigsOK。@Haradzieniec您为
401错误
定义了一个单独的文档了吗?@Haradzieniec之所以有效,是因为,
RewriteCond%{REQUEST\u URI}/administration/[NC]
添加到第2和第3块(直接指向http
协议),检查URI中是否没有
administration
。我理解更新代码的工作原理(非常感谢)。我刚刚创建了401错误页面。是的,它转到401错误页,而不是404。我仍然不明白为什么它会重定向到401错误页面?另外,如果我键入(http),它会首先询问密码,然后在输入正确的密码后显示401.shtml。但是,如果我键入(现在使用httpS),它将显示而不询问密码(每次在新浏览器中测试,以确保多次)。@Haradzieniec请确保您已正确设置
.htpasswd
身份验证。感谢您的解释!仔细阅读。谢谢你的解释!仔细阅读。