3

I have a linux instance hosting a WordPress site and it has Basic HTTP Auth enabled for the entire site. I now want to disable the Basic HTTP Auth for a specific path we can call /auth-free-url-2019.

My first thought was to set an ENV variable in the .htaccess file as follows:

SetEnvIf REQUEST_URI "^/auth-free-url-2019" allowed_page=1

and then adding the following into my auth

AuthUserFile SOME_AUTH_FILE
AuthName "AUTH NAME"
AuthType Basic
Order deny,allow
Deny from all
Satisfy Any
Require valid-user
Allow from all

This didn't work and I read about something about mod_rewrite possibly conflicting with the environment variables so I decided to try something simpler by adding an <If> directive into my .htaccess file as follows:

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

This is also not working and I added in a Header set to view the value of the REQUEST_URI and it appears to be correct.

In the <If> directive I've tried

"%{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}"

and only the last one will turn off the Basic Auth. I'm not sure what the problem is but for some reason, my attempts at comparing to the REQUEST_URI value is not working. I've tried the same <If> directive block but for the HOST_NAME and that seems to work so it's something to do with the REQUEST_URI.

Any help would be appreciated.

Apache 2.4.37

RHEL

and here's my .htaccess file

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

sparky
  • 31
  • 1

0 Answers0