Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
htaccess重写规则:根据键入的URL将http重定向到https或https重定向到http(双向)_Http_.htaccess_Mod Rewrite_Redirect_Https - Fatal编程技术网

htaccess重写规则:根据键入的URL将http重定向到https或https重定向到http(双向)

htaccess重写规则:根据键入的URL将http重定向到https或https重定向到http(双向),http,.htaccess,mod-rewrite,redirect,https,Http,.htaccess,Mod Rewrite,Redirect,Https,有谁能帮我写一个重写规则,重定向httphttps(返回和强制取决于键入的URL),条件如下: 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) 所有rest页面始终为https,即使键入的是http而不是https 下面的代码

有谁能帮我写一个重写规则,重定向httphttps(返回和强制取决于键入的URL),条件如下:

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) 所有rest页面始终为https,即使键入的是http而不是https

下面的代码将除https之外的所有URL(并保留参数)重定向到https

#redirects http to https if there are parameters
RewriteCond %{HTTPS} off
RewriteCond %{QUERY_STRING} !^$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
我尝试在上面的代码之后添加下面的代码,以将https重定向到http(如果没有参数),这样所有页面都始终是https,除了www.mydomain.com,但我没有运气。我还错过了?p=home,?p=home1,?qqq=home-我不知道如何添加它们

RewriteCond %{HTTP} off
RewriteCond %{QUERY_STRING} ^$
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}

尝试将以下内容添加到站点根目录中的htaccess文件中

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,L=301]

#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,L=301]

尝试将以下内容添加到站点根目录中的htaccess文件中

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,L=301]

#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,L=301]

哇!似乎我理解这个原则,但我不会从头开始自己写。人们怎么能在不到一年的时间里回答我的问题呢?:)另一个问题是:你能解释一下“家”吗。它是指最后一个符号(“1”)无效的“home1”还是“home1”?非常感谢。第二个问题:[E=IS_HTTP:1]或[ENV=IS_HTTP:1]?谢谢。@Haradzieniec
home1?
匹配
home
home1
使
1
成为可选
E
env
将起作用。非常感谢。它是有效的,但是。。。。我不知道这是不是这个话题的问题,不是另一个。。。在您的代码中,https是红色的,在Chrome中被划掉,在FF中也会发生类似的错误。一旦我删除了您的代码,一切正常,SSL证书显示良好-它是绿色的。它甚至可以很好地处理代码的第一个和第三个块(但如果没有第二个块,则以“RewriteCond%{HTTPS}on”开头)。原因可能是什么?非常感谢。Chrome中关于交叉https的问题在这里:图片在文件夹中。关于Ulrich Palha建议的重写(他是对的),文件夹中的图片将始终通过http加载。这就是为什么https页面有错误。哇。。。似乎我理解这个原则,但我不会从头开始自己写。人们怎么能在不到一年的时间里回答我的问题呢?:)另一个问题是:你能解释一下“家”吗。它是指最后一个符号(“1”)无效的“home1”还是“home1”?非常感谢。第二个问题:[E=IS_HTTP:1]或[ENV=IS_HTTP:1]?谢谢。@Haradzieniec
home1?
匹配
home
home1
使
1
成为可选
E
env
将起作用。非常感谢。它是有效的,但是。。。。我不知道这是不是这个话题的问题,不是另一个。。。在您的代码中,https是红色的,在Chrome中被划掉,在FF中也会发生类似的错误。一旦我删除了您的代码,一切正常,SSL证书显示良好-它是绿色的。它甚至可以很好地处理代码的第一个和第三个块(但如果没有第二个块,则以“RewriteCond%{HTTPS}on”开头)。原因可能是什么?非常感谢。Chrome中关于交叉https的问题在这里:图片在文件夹中。关于Ulrich Palha建议的重写(他是对的),文件夹中的图片将始终通过http加载。这就是https页面出现错误的原因。