Thanks for the answer above, it will help create a unified solution https and www. My only problem is that there are certain conditions under which auth does not start, allowing someone to access without credentials. Iām not sure what it is, but maybe you, bright people, can say otherwise.
This code redirects non-www to www and http to https, with .htaccess auth.
This is the contents of the htaccess file in the directory that you want to protect:
RewriteEngine on # ensure www. RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/foldername/$1 [L,R=301] # ensure https RewriteCond %{HTTP:X-Forwarded-Proto} !https RewriteCond %{HTTPS} !=on [NC] RewriteRule ^(.*)$ https://%{HTTP_HOST}/foldername/$1 [L,R=301] # Apache 2.4 If <If "%{HTTPS} == 'on' && %{HTTP_HOST} =~ /www/"> AuthType Basic AuthName "Protected folder" AuthUserFile "/home/etc/.htpasswds/public_html/foldername/passwd" require valid-user </If>
source share