Search code examples
html.htaccess.htpasswd

How to use.htaccess and .htpasswd to protect a specific web page in the website


Hi I've currently got a website for a restaurant where I need to have the staff.html page protected by .htaccess and .htpasswrd but I'm unsure how to do this as my main page is idex.html and has a link in the nav bar to the staff.html page.

Below is my .htaccess code:

AuthType Basic
AuthName "Restricted access"
AuthUserFile "\Users\tayla\OneDrive\Documents\BIT607\HaukaiA3\web.htpasswd"
Require valid-user

All of my files are saved within the same file as seen below:

"\Users\tayla\OneDrive\Documents\BIT607\HaukaiA3\web.htpasswd"

If anyone can help me understand how I can protect a specific page like my staff.html that would be muchly appreciated.


Solution

  • You can do it like this:

    # for URI=/staff.html set an env variable PROTECTED=1
    SetEnvIfNoCase Request_URI "/staff\.html" PROTECTED
    
    # allow all pages but put PROTECTED=1 behind basic auth
    AuthType Basic
    AuthName "Restricted access"
    AuthUserFile "\Users\tayla\OneDrive\Documents\BIT607\HaukaiA3\web.htpasswd"
    Require valid-user
    Satisfy    any
    Order      Allow,Deny
    Allow from all
    Deny from env=PROTECTED