1

I have a wordpress page and want to add basic auth to the page, except for the wordpress rest api (url: localhost/wp-json/xyz)!

The main problem seems to be the redirect that happens in the standard .htaccess of wordpress. Because of that I dont have access to the original request uri.

Here is the .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>

# END WordPress

Here an example: I added these lines to my virtualhost conf:

SetEnvIf Request_URI "(^.*$)" RURI=$1
CustomLog "logs/custom.log" "%U - %{RURI}e"

And the output when I visit localhost/wp-json will be:

/wp-json - /index.php

So the request_uri is always index.php, I would need the value that is in "%U", but how can I get that?

I also tried to add a location directive:

<Location ~ "^/(?!wp-json)">
    AuthType Basic
    AuthName "Restricted Files"
    # (Following line optional)
    AuthBasicProvider file
    AuthUserFile "C:\Users\moe\passwd"
    Require user user
</Location>

But this is also not working...

edit:

I also tried to add an environment variable in the .htaccess like this:

# 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,E=REQUEST_URI_ORIG:%1]
</IfModule>

# END WordPress

But also no success

I am using Apache/2.4.38

Anyone got an idea?

0 Answers0