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

时间:2012-02-07 15:55:03

标签: .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/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)这个代码所以它的工作原理? (我仍然没有运气,需要帮助)。 谢谢。

3 个答案:

答案 0 :(得分:2)

试试这个: 在第2和第2行添加第一行第3块。

即。 RewriteCond %{REQUEST_URI} !/administration/ [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]

答案 1 :(得分: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]

答案 2 :(得分:1)

规则的第二部分RewriteCond %{QUERY_STRING} ^$匹配任何没有查询字符串的网址。您的网址//www.mydomain.com/administration/any_file_in_admin_folder.php没有查询字符串。因此IS_HTTP被设置为1,并且您的用户被重定向到HTTP。

试试这个。这是未经测试的 - 但基本上你首先要识别“home”查询字符串,然后分别处理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]