I'm a designer who developed a simple site with HTML and CSS. I'm completely new to .htaccess so it's not surprising I'm struggling to get the file to work as I want.

Problem 1: I want to password protect my site so I created an .htaccess file with this code:

AuthType Basic
AuthName "Please enter password to proceed"
AuthUserFile /home/public_html/.htpasswd
require valid-user

It works great, except on mobile (testing on iphone 6s, chrome browser), where I'm not given the option to enter username or password, instead I get

"Unauthorized The server could no verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g. bad password), or your browser doesn't understand how to supply the credentials requested.

Additionally, a 401 Unauthorized error was encountered while trying to use an ErrorDocument to handle the request."

Problem 2: I also have gotten an SSL certificate for the site and am using this bit of code for the redirects.

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?example.com
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

It works fine, but when I attempt to combine it with the password protection code, bad things happen.

I would love instruction on how to make the two bits work together, and if anyone can also tell how to make the password protection work on mobile, I would be eternally grateful.

  • 3
    You should make sure that the HTTP authorization stuff (first part) happens only for the HTTPS virtualhost, if the HTTP one is purely a redirect to its HTTPS brother. Oct 4 '18 at 20:22
  • Thank you Patrick. Is there any way you can tell me how to go about that? (or point me to the right online instruction or tutorial?)
    – bcraig
    Oct 5 '18 at 17:39
  • I recommend not using .htaccess when you have access to the main configuration and just put everything there, properly in the associated VirtualHost block. Oct 9 '18 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.