0

I use a .htaccess basic auth on my WC site to help prevent hackers accessing wp-login, which works well... except with WooCommerce if a logged in customer wants to logout from their account - upon clicking the logout link - they are greeted by the Basic Auth popup asking them to "authorise" (generated by our .htaccess).

On Woocommerce dashboard:

Hello MrTest (not MrTest ? Log out) << clicking on "Log out" brings up the Basic Auth login box.... how can we avoid that ?

Here is the content of our htaccess:

<FilesMatch "wp-login.php">
AuthName "Authorized"
AuthType Basic
AuthUserFile /home/user/.pswrdfile
require valid-user
</FilesMatch>

In WooCommerce settings, the Logout endpoint is: customer-logout and the logout link URL shows:

example.com/shop/my-account/customer-logout/?_wpnonce=2e343434

So how to change the .htaccess to allow wp-login.php?action=logout to pass through the basic auth?

I tried this but it fails to work; I have Apache server with latest versions.

 RewriteEngine On
 RewriteCond %{REQUEST_URI} ^/wp-login.php$
 RewriteCond %{QUERY_STRING} ^action=logout
 RewriteRule ^ - [E=noauth]

 <FilesMatch "^(wp-login.php)">

    Options -Indexes +FollowSymLinks +MultiViews
    AuthName "Protected page. If you are not allowed to be here, leave the page"
    AuthType Basic
    AuthUserFile "/etc/apache2/htaccess/myhtaccess"
    Require valid-user

    Order Deny,Allow
    Deny from all
    Allow from env=noauth

    Satisfy any

 </FilesMatch>
2
  • What version of Apache are you on?
    – MrWhite
    Nov 19, 2019 at 13:55
  • @MrWhite Apache 2.4 Nov 19, 2019 at 20:30

1 Answer 1

0

From the answer in Password protect a specific URL, we can reverse the condition. To quote his answer,

You should be able to do this using the combination of mod_env and the Satisfy any directive. You can use SetEnvIf to check against the Request_URI, even if it's not a physical path. You can then check if the variable is set in an Allow statement. So either you need to log in with password, or the Allow lets you in without password:

# Do the regex check against the URI here, if match, set the "require_auth" var
SetEnvIf Request_URI ^/shop/my-account/customer-logout/ require_auth=false

# Auth stuff
AuthUserFile /var/www/htpasswd
AuthName "Password Protected"
AuthType Basic

# Setup a deny/allow
Order Deny,Allow
# Deny from everyone
Deny from all
# except if either of these are satisfied
Satisfy any
# 1. a valid authenticated user
Require valid-user
# or 2. the "require_auth" var is NOT set
Allow from env=!require_auth

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.