我有一个托管 WordPress 站点的 Linux 实例,并且它为整个站点启用了基本 HTTP 身份验证。我现在想禁用特定路径的基本 HTTP 身份验证,我们可以调用/auth-free-url-2019
。
我的第一个想法是在文件中设置一个 ENV 变量,.htaccess
如下所示:
SetEnvIf REQUEST_URI "^/auth-free-url-2019" allowed_page=1
然后将以下内容添加到我的身份验证中
AuthUserFile SOME_AUTH_FILE
AuthName "AUTH NAME"
AuthType Basic
Order deny,allow
Deny from all
Satisfy Any
Require valid-user
Allow from all
这不起作用,我读到一些关于 mod_rewrite 可能与环境变量冲突的内容,所以我决定尝试一些更简单的方法,<If>
在我的 .htaccess 文件中添加一条指令,如下所示:
<If "%{REQUEST_URI} =~ m#auth-free-url-2019#i">
Order allow,deny
Satisfy Any
Allow from all
Deny from none
</If>
<Else>
Header set Checked-Request-url %{REQUEST_URI}e
AuthUserFile SOME_AUTH_FILE
AuthName "AUTH NAME"
AuthType Basic
Order deny,allow
Deny from all
Satisfy Any
Require valid-user
</Else>
这也不起作用,我添加了一个来Header set
查看其值REQUEST_URI
,它看起来是正确的。
在<If>
我尝试过的指令中
"%{REQUEST_URI} =~ m#auth-free-url-2019#i"
"%{REQUEST_URI} =~ /\/auth-free-url-2019$/i"
"%{REQUEST_URI} =~ /.*auth-free-url-2019.*/i"
"%{REQUEST_URI} == '/auth-free-url-2019'"
"%{REQUEST_URI} == 'auth-free-url-2019'"
"%{REQUEST_URI} == 'auth-free-url-2019'"
"-n %{REQUEST_URI}"
只有最后一个会关闭基本身份验证。我不确定问题是什么,但出于某种原因,我尝试与 REQUEST_URI 值进行比较,但没有成功。我尝试了相同的<If>
指令块,但针对的是 HOST_NAME,似乎有效,所以这与 REQUEST_URI 有关。
任何帮助,将不胜感激。
Apache 2.4.37
红帽企业版
这是我的 .htaccess 文件
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
<If "%{REQUEST_URI} =~ m#auth-free-url-2019#i">
Order allow,deny
Satisfy Any
Allow from all
Deny from none
</If>
<Else>
AuthUserFile SOME_AUTH_FILE
AuthName "AUTH NAME"
AuthType Basic
Order deny,allow
Deny from all
Satisfy Any
Require valid-user
#Allow from all
</Else>
# Exclude the WP CRON and other scripts from authentication
<FilesMatch "(wp-cron.php|another-script.php)$">
Satisfy Any
Order allow,deny
Allow from all
Deny from none
</FilesMatch>
# BEGIN W3TC Page Cache core
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} =on
RewriteRule .* - [E=W3TC_SSL:_ssl]
RewriteCond %{SERVER_PORT} =443
RewriteRule .* - [E=W3TC_SSL:_ssl]
RewriteCond %{HTTP:X-Forwarded-Proto} =https [NC]
RewriteRule .* - [E=W3TC_SSL:_ssl]
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteRule .* - [E=W3TC_ENC:_gzip]
RewriteCond %{HTTP_COOKIE} w3tc_preview [NC]
RewriteRule .* - [E=W3TC_PREVIEW:_preview]
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{QUERY_STRING} =""
RewriteCond %{HTTP_COOKIE} !(comment_author|wp\-postpass|w3tc_logged_out|wordpress_logged_in|wptouch_switch_toggle) [NC]
RewriteCond "%{DOCUMENT_ROOT}/wp-content/cache/page_enhanced/%{HTTP_HOST}/%{REQUEST_URI}/_index%{ENV:W3TC_SSL}%{ENV:W3TC_PREVIEW}.html%{ENV:W3TC_ENC}" -f
RewriteRule .* "/wp-content/cache/page_enhanced/%{HTTP_HOST}/%{REQUEST_URI}/_index%{ENV:W3TC_SSL}%{ENV:W3TC_PREVIEW}.html%{ENV:W3TC_ENC}" [L]
</IfModule>