30

这是我的 .htaccess:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

AuthUserFile /etc/hi
AuthName "hi"
AuthType Basic
require valid-user

它要求使用 http 进行用户身份验证,这意味着密码将以纯文本形式发送。它将重定向到 https 版本并再次询问密码。

我该如何解决?

4

9 回答 9

23

如果您正在运行 Apache 2.4,您可以使用配置部分来轻松解决这个问题。

例如...

# Redirect to HTTPS
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

# Authenticate users only when using HTTPS
# Enable for <v2.4
 # SSLRequireSSL
 # ErrorDocument 403 /secure-folder/
# Enable for >v2.4
<If "%{HTTPS} == 'on'">
    AuthType Basic
    AuthName "Special things"
    AuthUserFile /etc/blah.htpasswd
    # Prevent this 'Require' directive from overriding any merged previously
   <IfVersion >= 2.4>
      AuthMerging And
   </IfVersion>
    Require valid-user
# Enable for >v2.4
</If>
于 2014-10-02T05:45:00.560 回答
15

我以这种方式绕过它。只允许非 SSL,因为它将被重定向,然后需要在 SSL 上进行一次身份验证...

SetEnvIf %{SERVER_PORT} ^80$ IS_NON_SSL

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

AuthUserFile /etc/hi
AuthName "hi"
AuthType Basic
require valid-user
Allow from env=IS_NON_SSL
于 2013-04-11T03:46:09.753 回答
12

非常感谢,伊斯塔多!

我的 Apache 是 2.2 版(Synology NAS DSM 5.1),所以这两个不适用:

RewriteOptions Inherit
IfVersion

将它们(以及版本> = 2.4的部分)取出后。整个事情开始为我工作。

关于这个主题有很多建议,我花了两天时间尝试一下。

但只有这个对我有用。

这是我所做的:

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

AuthType Basic
AuthName "private area"
AuthUserFile /path/to/file/.htdigest

Order Deny,Allow
Deny from all
Satisfy Any
Allow from env=!HTTPS
Require valid-user

因此,它经过验证可以在 Apache 2.2、Synology DSM 5.1 上运行。

于 2015-05-10T07:56:44.177 回答
4

检查的解决方案https://stackoverflow.com/a/15940387/2311074在 Ubuntu 16.04 上的 Firefox 上工作,但在 Win 7 上的 Firefox 上不起作用。

如果您想保护您的文件夹https://yourdomain.com/securefolder ,那么您需要在该文件夹中创建.htaccess具有以下内容的 a:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

SSLRequireSSL
ErrorDocument 403 https://yourdomain.com/securefolder
AuthType Basic
AuthName "Admin"
AuthUserFile /outside/your/www/folder/.htpasswd
Require user admin Admin

它的工作方式是,当您通过http://而不是通过https://它调用网站时,会将您重定向到错误页面。诀窍是使用正确的链接https://作为默认错误页面。

于 2017-04-19T09:06:38.943 回答
2

我们客户的 webapp 安装在他的 webuser 目录中。授权是在 mod_rewrite 规则(https://serverfault.com/a/443185/253111)之前处理的,我们无法得到公认的答案,所以 mod_rewrite 似乎不是一个选项。

最终,我们明确要求 SSL,并通过 HTTPS 使用 webapp 的根作为 403 和 404 错误文档。因此,当一个人通过 HTTP(未经授权,因此是 403)或不存在的页面(404)访问任何页面时,他将被重定向到 ie。https://DOMAIN.TLD/~WEBUSER/admin

这是 .htaccess 文件,评论中有一些额外的信息。

### INFO: Rewrites and redirects are handled after authorisation
### @link https://serverfault.com/a/443185/253111

### INFO: Log out of a HTPASSWD session
### This was not always possible, but Firefox and Chrome seem to end sessions
### when a new one is trying to be using ie.:
### https://logout:logout@DOMAIN.TLD/~WEBUSER/
### @link http://stackoverflow.com/a/1163884/328272

### FORCE SSL: Explicitly require the SSL certificate of a certain domain to
### disallow unsigned certificates, etc. ErrorDocument commands are used to
### redirect the user to an HTTPS URL.
### @link http://forum.powweb.com/showthread.php?t=61566
SSLOptions +StrictRequire
SSLRequireSSL
SSLRequire  %{HTTP_HOST} eq "DOMAIN.TLD"

### HTPASSWD AUTHENTICATION
AuthUserFile /var/www/vhosts/DOMAIN.TLD/web_users/WEBUSER/.htpasswd
AuthType Basic
AuthName "Hello"
Require valid-user

### ERROR DOCUMENTS: Redirect user in case of a 403 / 404.
ErrorDocument 403 https://DOMAIN.TLD/~WEBUSER/admin
ErrorDocument 404 https://DOMAIN.TLD/~WEBUSER/admin
于 2015-02-17T11:55:00.437 回答
2

我正在运行 Apache 2.2,但上述解决方案都不适合我。我在这里找到了适合我的解决方法。基本上,您需要设置 SSLRequireSSL 并在 ErrorDocument 中使用一些脚本语言将用户转发到 HTTPS。不幸的是,在我的情况下,这仅在访问服务器上的特定文件时有效,如果仅提供域,则它不起作用。这是我所做的:

AuthType Basic
AuthName "Password Protected Area"
AuthUserFile /my/path/to/.htpasswd
#Require valid-user

<FilesMatch "(^(?!ssl.php).*)">
        SSLRequireSSL
        ErrorDocument 403 /ssl.php
        Require valid-user
</FilesMatch>

FileMatch 中的正则表达式告诉 apache 对除 ssl.php 之外的所有文件使用 SSLRequireSSL - 如果用户尝试在没有 SSL 的情况下访问,则将用户转发到 ssl.php。

我的 ssl.php 看起来像这样:

if(!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == "" || $_SERVER['HTTPS'] == "off")
{
        $redirect = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        header("HTTP/1.1 301 Moved Permanently");
        header("Location: $redirect");
        exit;
}

现在会发生什么:

最后一点是我不满意的,如果有人对此有解决方案,我会很高兴听到。我试图解决这个问题的事情:

  • 将正则表达式更改为 (^$)|(^(?!ssl.php).*) 以显式匹配空字符串。不工作
  • 添加了重写规则以将空字符串重写到 index.php。也不行。
于 2015-12-17T09:41:20.653 回答
2

这是在我的一种配置中起作用的唯一解决方案:

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

<If "%{SERVER_PORT} != '80'">
    AuthUserFile /etc/hi
    AuthName "hi"
    AuthType Basic
    require valid-user
</If>

值得注意的是,这是针对我无法控制的 Apache 2.4(共享主机)。似乎%{HTTPS}未在此配置上定义变量,并且任何基于SSLRequireSSL生成 500 Internal Server Error 的解决方案。


(旁注:如果您在服务 HTTP 请求时更喜欢 403 Forbidden 而不是 301 Permanent Redirect,请RewriteRule ^(.*)$ - [F,L]改用)

于 2019-01-22T18:16:23.870 回答
1

Molomby 的解决方案适用于 2.4 及更高版本,但不适用于当前的 Debian 版本 2.2.22。

Ben's / Chris Heald 的解决方案在 2.2.22 中也不适用于我,但这是由于不同的 order/satisfy 配置。这些设置已随 2.4 更改,并且该解决方案似乎与 2.4 及更高版本不兼容(重定向有效,但浏览器只是显示未经授权的错误而不要求提供凭据)。

以下是适用于 2.4 以下和以上版本的两种解决方案的组合:

RewriteEngine on
RewriteOptions Inherit # rewrite rules from parent directories
RewriteCond %{HTTPS} off
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

AuthType Digest
AuthName "private area"
AuthDigestProvider file
AuthUserFile /path/to/file/.htdigest

<IfVersion < 2.4>
    Order Deny,Allow
    Deny from all
    Satisfy Any # reset this to 'All' in custom <Files> and <Directory> directives that block access
    Allow from env=!HTTPS
    Require valid-user
</IfVersion>
<IfVersion >= 2.4>
    <If "%{HTTPS} == 'on'">
        AuthMerging And
        Require valid-user
    </If>
</IfVersion>

要求:mod_rewrite、mod_auth、mod_digest、mod_version

于 2015-02-19T05:36:21.263 回答
1

以上都不适合我,但确实如此。我唯一担心的是,如果在某些情况下不会触发身份验证,则允许某人在没有凭据的情况下进行访问。我不确定是否有,但也许你们聪明的人可能会说不同。

此代码使用 .htaccess 文件夹身份验证将非 www 重定向到 www 并将 http 重定向到 https。

这是您要保护的目录中的 htaccess 文件的内容:

RewriteEngine on
# ensure www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/foldername/$1 [L,R=301]
# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} !=on [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/foldername/$1 [L,R=301]

# Apache 2.4 If
<If "%{HTTPS} == 'on' && %{HTTP_HOST} =~ /www/">
AuthType Basic
AuthName "Protected folder"
AuthUserFile "/home/etc/.htpasswds/public_html/foldername/passwd"
require valid-user
</If>
于 2018-02-08T06:42:43.703 回答