【问题标题】:.htaccess: redirect http to https or https to http depending on URL.htaccess:根据 URL 将 http 重定向到 https 或 https 到 http
【发布时间】:2012-02-08 00:39:32
【问题描述】:

以下解决方案涵盖了必需的规则:

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) 以使其正常工作? (我仍然没有运气,需要帮助)。 谢谢。

【问题讨论】:

    标签: .htaccess mod-rewrite https flags


    【解决方案1】:

    试试这个: 在第二和第三块中添加第一行。

    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]
    

    【讨论】:

    • 谢谢。它有效(您的答案是我尝试过的第一个)。任何人都可以猜到它为什么有效,但是一旦我创建了密码(我已经尝试了两种方法 - 使用托管面板设置 - >通过密码保护文件夹以及创建 .htaccess 和 .htpasswd 文件),页面管理/索引。 php 重定向到 404 错误页面。 IE 说(如果我输入了错误的密码):此外,在尝试使用 ErrorDocument 处理请求时遇到了 404 Not Found 错误。如果我输入正确,则显示 404 错误页面,没有错误。这是怎么回事?没有密码 -everythigsOK。
    • @Haradzieniec 你为401 error定义了一个单独的文档吗?
    • @Haradzieniec 之所以有效,是因为,RewriteCond %{REQUEST_URI} !/administration/ [NC] 添加在第二和第三块(直接指向http 协议)检查URI 中没有administration
    • 我明白了,为什么更新的代码有效(非常感谢)。我刚刚创建了 401 错误页面。是的,它转到 401 错误页面,而不是 404。我仍然不明白为什么它会重定向到 401 错误页面?此外,发生的情况是:如果我输入mydomain.com/administration/index.php (http),它会先询问密码,输入正确密码后会显示 401.shtml。但是,如果我输入mydomain.com/administration/index.php(现在使用httpS),它会在不询问密码的情况下显示mydomain.com/401.shtml(每次都在新浏览器中测试,可以肯定的是多次)。
    • @Haradzieniec 确保您已正确设置 .htpasswd 身份验证。
    【解决方案2】:

    将您的第一条规则更改为:

    #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]
    

    【讨论】:

    • 很抱歉,它不起作用。管理文件夹中的任何文件仍会转到 http(也可以从 https)。
    • 对不起,我之前犯了一个错误,刚刚修复它。请尝试。
    【解决方案3】:

    规则的第二部分,RewriteCond %{QUERY_STRING} ^$ 匹配任何没有查询字符串的 URL。您的 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]
    

    【讨论】:

    • 感谢您的解释!请仔细阅读。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-23
    • 2012-11-02
    • 1970-01-01
    • 1970-01-01
    • 2015-11-02
    • 1970-01-01
    相关资源
    最近更新 更多