Search code examples
htmlwordpress.htaccessauthentication.htpasswd

How to protect a directory withing a Wordpress installation with htaccess?


I have a wordpress installation locatet at /root/website/ and a seperate, non-wordpress subpage at /root/website/subpage/ so I can simply access it by www.domain.com/subpage

I want to have the subpage protected with a htaccess file but I always get a internal server error (500) after entering the login details.

Isn't it possible to have two htaccess files or do I have to edit the Wordpress htaccess file to protect the subpage directory?

My Worpress installation has a htaccess in it's root directory containing:

# 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

Then I have the following htaccess in my subpage directory:

AuthType Basic
AuthName "NameHere"
AuthUserFile /root/website/subpage/.htpasswd
Require valid-user

And of course I have set up a htpasswd file using a generator online.


Solution

  • Add to .htaccess file in public_html or publicDirectory:

    You can set what directory do you want allowed, with example from below:

    <Directory /root/website>
    # All access controls and authentication are disabled
    # in this directory
    Satisfy Any
    Allow from all
    </Directory>
    
    <Directory /root/website/subpage>
    AuthType Basic
    AuthName "NameHere"
    AuthUserFile /root/website/subpage/.htpasswd
    Require valid-user
    </Directory>
    
    # 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