Use HTTP Auth only if accessing a specific domain

前端 未结 5 1423
青春惊慌失措
青春惊慌失措 2020-12-30 06:02

I\'ve got several sites: example.com, example1.com, and example2.com. All of them point to my server\'s /public_html fo

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-30 06:49

    How about something along the lines of this in the htaccess file in the document root:

    # set the "require_auth" var if Host ends with "example2.com"
    SetEnvIfNoCase Host example2\.com$ require_auth=true
    
    # 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
    

    This will make it so authentication is not required unless the host ends with example2.com (e.g. www.example2.com, dev.example2.com, etc). The expression can be tweaked if needed. Any other host will cause the require_auth var not to get set so authentication is not required. If this needs to be the other way around, the last line could be changed to: Allow from env=require_auth, removing the !.

提交回复
热议问题