I have a WordPress site, where I grant access to users through an O2Auth provider, so they never have to query wp-login.php
. But, when users log out, they need to query for wp-login.php?action=logout&_wpnounce=dead0beef
I want to protect wp-login
via .htaccess, Require valid-user
So can I add a Require
directive to also allow action=logout
?
I tried these without success:
SetEnvIf Request_URI "o2a-login" o2a-user
...
Require env o2a_user
Require expr "(%{QUERY_STRING} =~ logout)"
Require expr "%{QUERY_STRING} in { 'action=logout(.*)', 'action=logout', '.*logout.*' }"
Require expr "%{QUERY_STRING} in {'wp-login.php?action=logout.*'}"
Or as an alternative way – Can I respect wp-logged-in users at Apache?
2
Answers
Ok, I don't know if that's a very good solution but it works for me:
ln -s wp-login.php mySecretLogin.php
mySecretLogin
as referrermySecretLogin
by authenticationand
This way the
wp-login.php
is only allowed for log-out or viamysite.tld/mySecretLogin.php
with a password.If you simply want to allow the
action=logout
URL parameter (at the start of the query string) and block all other requests towp-login.php
then you can use the following mod_rewrite directives before the WordPress front-controller, near the top of the.htaccess
file:The above states, for all requests to
/wp-login.php
that do not haveaction=logout
at the start of the query string then send a 403 Forbidden.You can’t determine whether a user is actually logged in to WP from
.htaccess
. You can check that a cookie is set, but you can’t authenticate the value of that cookie (with the WP session) – so anyone could potentially set that cookie.You could potentially check for a "private" (known only to you) query string to perhaps obfuscate the login.
UPDATE:
You could perhaps set a cookie when passing your secret key in the URL and use this cookie to actually authorise the request instead (survives redirects)? For example:
The
%N
backreferences in theRewriteRule
substitution string and flags arguments refer to the captured subpatterns in the preceding CondPattern.The "obfuscation string", ie.
secret-name=secret-value
pair, is used to set the cookie on a redirect. So the cookie can be immediately read (and authorise access) on the redirected request. The redirect also removes this "obfuscation string" from the visible query string.Note that the session cookie is only set on secure HTTPS connections and is not available to JavaScript.
Alternatively, it would be more secure/easier to authorise by client IP address, but if you have a dynamic IP that may not be practical. For example:
203.0.113.111
is your external IP address. Note that since we’re using the=
prefix on the CondPattern this is an exact string match (not a regex), so the dots do not need to be backslash escaped.