2

I need to protect one directory with the basic auth and rewrite all urls inside this directory only.

I have the following config:

    location /admin/ {
            auth_basic "Secure area";
            auth_basic_user_file .htpasswd;
            rewrite  ^(.*)$ /admin/index.php last; break;
    }

However, basic auth not working. If I comment rewrite rule it starts working.

How to fix it?

Ssey
  • 21
  • 1
  • 4

1 Answers1

2

"rewrite" goes before basic auth, because of request workflow.

try trick with error page and named location:

 location /admin/ {
            auth_basic "Secure area";
            auth_basic_user_file .htpasswd;
            error_page 404 = @admin;
    }

location @admin {
            rewrite  ^(.*)$ /admin/index.php last;
}
user395994
  • 21
  • 2
  • Thank you. However, it works partly. Basic auth works, but the rewrite rule does not. After logging, if I try to refresh the page it redirects me back to admin/index.php instead of staying on the dynamic page. If I remove your code, it works exactly as it is supposed to. – Ssey Jan 25 '17 at 09:41