2012-02-07 121 views
0

溶液下方覆盖所需的规则:htaccess的:重定向HTTP到HTTPS或https为http取决于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)即使输入的是http而不是https,其余所有页面始终为https,

但在实践中不涵盖

3)//www.mydomain.com/administration/any_file_in_admin_folder.php应该总是HTTPS以及(即使有参数P =家庭等)。

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

回答

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] 
+0

谢谢。它的作品(你的答案是我试过的第一个)。任何人都可以猜测,为什么它有效,但是一旦我创建了一个密码(我已经尝试了两种方式 - 使用主机面板设置 - >通过密码保护文件夹,还创建.htaccess和.htpasswd文件)页面管理/索引。 PHP重定向到404错误页面。 IE说(如果我输入了错误的密码):另外,尝试使用ErrorDocument来处理请求时遇到404 Not Found错误。如果我输入正确,404错误页面显示没有错误。这是怎么回事?没有密码--everythigsOK。 – Haradzieniec 2012-02-07 20:05:53

+0

@Haradzieniec是否为'401错误'定义了单独的文档? – ThinkingMonkey 2012-02-07 20:18:46

+0

@Haradzieniec它的工作原理是在第二和第三块(指向'http'协议)中添加'RewriteCond%{REQUEST_URI}!/ administration/[NC]'来检查URI中没有'administration'。 – ThinkingMonkey 2012-02-07 20:20:30

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] 
+0

对不起,这是行不通的。管理文件夹中的任何文件仍然会转到http(也可能来自https)。 – Haradzieniec 2012-02-07 16:10:58

+1

对不起,我之前犯了一个错误,只是修正了它。请尝试。 – anubhava 2012-02-07 16:30:11

1

您的规则,RewriteCond %{QUERY_STRING} ^$匹配任何URL的第二部分没有查询字符串。您的网址//www.mydomain.com/administration/any_file_in_admin_folder.php没有查询字符串。所以IS_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] 
+0

谢谢你的解释!仔细阅读。 – Haradzieniec 2012-02-10 22:45:58