By KrzysDan


2012-01-23 20:26:26 8 Comments

I need to exclude one Url (or even better one prefix) from normal htaccess Basic Auth protection. Something like /callbacks/myBank or /callbacks/.* Do you have any hints how to do it?

What I'm not looking for is how to exclude a file. This has to be url (as this is solution based on PHP framework, and all urls are redirected with mod_rewrite to index.php). So there is no file under this URL. Nothing.

Some of those urls are just callbacks from other services (No IP is not known so I cannot exclude based on IP) and they cannot prompt for User / Password.

Current definition is as simple as:

AuthName "Please login."
AuthGroupFile /dev/null
AuthType Basic
AuthUserFile /xxx/.htpasswd
require valid-user 

8 comments

@beporter 2015-11-11 16:32:49

If you are using Apache 2.4, SetEnvIf and mod_rewrite workarounds are no longer necessary since the Require directive is able to interpret expressions directly:

AuthType Basic
AuthName "Please login."
AuthUserFile "/xxx/.htpasswd"

Require expr %{REQUEST_URI} =~ m#^/callbacks/.*#
Require valid-user

Apache 2.4 treats Require directives that are not grouped by <RequireAll> as if they were in a <RequireAny>, which behaves as an "or" statement. Here's a more complicated example that demonstrates matching both the request URI and the query string together, and falling back on requiring a valid user:

AuthType Basic
AuthName "Please login."
AuthUserFile "/xxx/.htpasswd"

<RequireAny>
    <RequireAll>
        # I'm using the alternate matching form here so I don't have
        # to escape the /'s in the URL.
        Require expr %{REQUEST_URI} =~ m#^/callbacks/.*#

        # You can also match on the query string, which is more
        # convenient than SetEnvIf.
        #Require expr %{QUERY_STRING} = 'secret_var=42'
    </RequireAll>

    Require valid-user
</RequireAny>

This example would allow access to /callbacks/foo?secret_var=42 but require a username and password for /callbacks/foo.

Remember that unless you use <RequireAll>, Apache will attempt to match each Require in order so think about which conditions you want to allow first.

The reference for the Require directive is here: https://httpd.apache.org/docs/2.4/mod/mod_authz_core.html#require

And the expression reference is here: https://httpd.apache.org/docs/2.4/expr.html

@Scott Smith 2016-01-10 22:34:20

I had to remove the double-quotes around the Require expr for it to work on apache 2.4.7. Other than that worked perfectly.

@TMPilot 2016-10-21 14:29:27

I too had to remove the double-quotes but it worked perfectly.

@Stephan Richter 2016-12-11 22:12:22

Does not work on my Apache 2.4.10 / cakephp 3 installation. I want to exclude /rest, which is a virtual path (rewrite rule). Can't figure out, why it's not working.

@beporter 2016-12-13 20:36:25

@stephan-richter I suspect because the %{REQUEST_URI} value is the rewritten string. For Cake 3, that ends up pretty much always being /webroot/index.php. You may have to come up with an alternative approach, such as using Cake itself to handle the authentication of routes.

@Stephan Richter 2016-12-14 23:04:39

You are right. Posted a question on this issue and found the answer myself: stackoverflow.com/questions/41077895/…

@Nicola Beghin 2018-02-04 20:44:28

Can't understand why this overly-simplified syntax Require expr %{REQUEST_URI} =~ m#.*well-known.*# does not exclude from authentication the URL http://<server>/.well-known/acme-challenge

@ryaan_anthony 2017-11-03 02:55:14

why don't you just use basic auth the way it was intended?

user:[email protected]/callbacks/etc

@frank42 2018-02-14 15:08:57

Well, because a) no one really wants™ auth data in the URI string, and b) because it's likely that OP has not the opportunity to customize the call (since he's talking about callbacks from a bank, I assume he's unable to change the behaviour on the client side)

@MagentoMan 2016-12-22 18:04:24

I tried the other solutions but this is what worked for me. Hopefully it will be of help to others.

# Auth stuff
AuthName "Authorized personnel only."
AuthType Basic
AuthUserFile /path/to/your/htpasswd/file

SetEnvIf Request_URI "^/index.php/api/*" allow
Order allow,deny
Require valid-user
Allow from env=allow
Deny from env=!allow
Satisfy any

This will allow the api url and any url string after /index.php/api/ to open without having to login and anything else will be prompted to login.

Example:

mywebsite.com/index.php/api will open without being prompted to login mywebsite.com/index.php/api/soap/?wsdl=1 will open without being prompted to login mywebsite.com will be prompted to login first

@Nits 2016-04-19 10:29:06

Add below code to your root htaccess file and don't forget to change your admin url, .htpasswd file page.

<Files "admin.php">
        AuthName "Cron auth"
        AuthUserFile E:\wamp\www\mg\.htpasswd
        AuthType basic
        Require valid-user
    </Files>

Create .htpasswd file in your root folder and add below username and password (set default username:admin and password: admin123)

admin:$apr1$8.nTvE4f$UirPOK.PQqqfghwANLY47.

Please let me know if you still facing any issue.

@Jon Lin 2012-01-23 23:01:00

Using SetEnvIf, you can create a variable when the request starts with some path, then use the Satisfy Any directive to avoid having to login.

# set an environtment variable "noauth" if the request starts with "/callbacks/"
SetEnvIf Request_URI ^/callbacks/ noauth=1

# the auth block
AuthName "Please login."
AuthGroupFile /dev/null
AuthType Basic
AuthUserFile /xxx/.htpasswd

# Here is where we allow/deny
Order Deny,Allow
Satisfy any
Deny from all
Require valid-user
Allow from env=noauth

The allow/deny chunk of directives says that deny access for EVERYONE, except when there is a valid-user (successful BASIC auth login) or if the noauth variable is set.

@vishal 2014-03-10 06:13:52

I added this section in <VirtualHost *:80> DocumentRoot /var/www/Symfony/web/ <Directory /var/www//Symfony/web> but its not working.

@asdmin 2015-08-03 06:48:56

Could you please explain, why <Files> does not work here?

@James F 2015-08-28 09:17:04

Thanks! Really helped me out, be careful of case - I had Order deny,allow and it wouldn't work. Needless to say very frustrating!

@Stephan Richter 2016-12-12 00:01:46

may require an Allow from env=REDIRECT_noauth for rewriten urls. Might have been the problem with @vishal. Have a look here: [stackoverflow.com/a/41092497/1285585]

@rob5408 2017-09-12 22:23:01

If your public HTML page <link>s to any other file on your server be sure to except it too. Took me way too long to figure out why I was still getting an auth dialog.

@CDuv 2018-07-16 14:31:05

I need both lines Allow from env=noauth and Allow from env=REDIRECT_noauth for it to work.

@bluszcz 2013-06-21 15:11:42

<location />
        SetEnvIf Request_URI "/callback/.*" REDIRECT_noauth=1

        AuthType Basic
        AuthName "Restricted Files"
        AuthUserFile /etc/httpd/passwords/passwords
        Order Deny,Allow
        Satisfy any
        Deny from all
        Allow from env=REDIRECT_noauth
        Require user yournickname
</location>

@kitchin 2013-02-17 15:34:05

Another approach works like this, if the area you are protecting has a monolithic PHP script controlling everything, like Wordpress. Set up Authentication with in a different directory. Put an index.php there that sets a cookie on path '/'. Then in Wordpress (for example), check the cookie, but bypass the check if $_SERVER['REQUEST_URI'] is the excluded URL.

On my shared hosting platform, RewriteRule could not set an environment variable that worked with "Satisfy any".

With any approach, watch out that the page you are protecting does not include images, stylesheets, etc., that trigger an authentication request when the page itself does not.

@Nick Z 2013-02-14 09:17:31

This solution works pretty well, you just need to define whitelist you want to pass through.

SetEnvIfNoCase Request_URI "^/status\.php" noauth

AuthType Basic
AuthName "Identify yourself"
AuthUserFile /path/to/.htpasswd
Require valid-user

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

Satisfy any

Related Questions

Sponsored Content

6 Answered Questions

9 Answered Questions

1 Answered Questions

disable http auth for specific rewritten location

2 Answered Questions

.htaccess access control

3 Answered Questions

[SOLVED] htacess file not working

  • 2011-05-23 18:32:41
  • user121196
  • 310 View
  • 1 Score
  • 3 Answer
  • Tags:   apache .htaccess

3 Answered Questions

[SOLVED] .htpasswd under IIS - does it work?

  • 2010-03-23 15:37:04
  • Cyrcle
  • 2852 View
  • 2 Score
  • 3 Answer
  • Tags:   iis .htaccess

Sponsored Content