3

我们有一个a.com受密码保护的 drupal 网站。不过,我希望所有a.com/api/...URI 都不是。所以我读过SetEnvIf

AuthName "Stage"
AuthType Basic
AuthUserFile ~/.htpasswd
SetEnvIf Request_URI ".*data_sheets.*\.pdf" noauth
SetEnvIf Request_URI "/api/.+" noauth
SetEnvIfNoCase Request_Method OPTIONS noauth
Order Deny,Allow
Deny from all
Require valid-user
Allow from env=noauth
Satisfy Any

不过,/api/foobarURI 仍然要求输入密码。由于它是一个 Drupal 网站,在 anubhava 的帮助下,我们认为它与 index.php 如何处理请求有关。

如何处理?

编辑

添加

RewriteCond %{REQUEST_URI} ^/api/ [NC]
RewriteRule ^ - [E=noauth]

紧接着

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]

没有帮助

4

3 回答 3

2

这对我有用:

AuthName "Stage"
AuthType Basic
AuthUserFile /var/www/html/.htpasswd
SetEnvIf Request_URI ".*data_sheets.*\.pdf" noauth
SetEnvIf Request_URI "/api/.+" noauth
SetEnvIfNoCase Request_Method OPTIONS noauth

RewriteEngine On
RewriteCond %{THE_REQUEST} \s/api/
RewriteRule ^ - [E=noauth:1]

Order Deny,Allow
Deny from all
Require valid-user
Allow from env=noauth
Allow from env=rewritten
Satisfy Any

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ /index.html [L]
于 2018-02-20T16:04:38.527 回答
2

我迟到了两年,但我已经对正在发生的事情以及如何解决它进行了彻底的解释。简短版本:

RewriteRule 由子请求完成。SetEnvIf 模块不继承子请求中的 REQUEST_URI 变量。您的 noauth 变量最终在这些子请求中未定义。

与 mod_core 一起使用<Location>和块,而不是依赖 mod_setenvif。<LocationMatch>SetEnv

于 2020-06-11T01:38:38.103 回答
1

出于某种原因,我在 3 年后访问了这个问题,并尝试发布一个可以在任何 Apache 版本中运行的有效解决方案。

您可以使用 Drupal 规则在您的站点根目录 .htaccess 中尝试这些规则:

DirectoryIndex index.php
RewriteEngine on

# remove /index.php from URLs
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ %1 [L,R=301,NE]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]

# allow anything that starts with /api/
SetEnvIfNoCase Request_URI ^/api/ noauth

<FilesMatch "^(?!index\.php$).*$">
   AuthType Basic
   AuthName "Stage"
   AuthUserFile ~/.htpasswd
   Require valid-user
   Order   Deny,Allow
   Deny from  all
   Allow from env=noauth
   Satisfy any
</FilesMatch>
于 2021-08-04T17:23:38.473 回答