You've brought your site up on hosting, and now a path like /staging or /admin-old is left open that no one should see. The fastest thing that comes to mind is password-protecting the folder. You create a file, write two lines in it, and the browser asks for a password. Done. So far so good; the problem starts when those same two lines also lock out the Google crawler, the HTTPS redirect, and your own scripts, and you think the site is broken.
Password-protecting a folder with htpasswd in four commands
On Apache and LiteSpeed, password protection is done via an .htaccess file and a separate password file. First, create the password file. If htpasswd is installed on the server:
htpasswd -c /home/USERNAME/.htpasswd staging
htpasswd /home/USERNAME/.htpasswd seconduser
The -c flag is only used the first time and recreates the file from scratch. If you run it a second time with -c, the previous user is deleted. I've seen this a lot. If you don't have SSH access, create the same file with an online generator and upload it with File Manager; just put it outside public_html so it can't be read from the web.
Now, in the target folder, create or edit the .htaccess file:
AuthType Basic
AuthName "Staging Area"
AuthUserFile /home/USERNAME/.htpasswd
Require valid-user
The AuthUserFile path must be absolute. A relative path is one of the most common mistakes, and the result is a 500 error, not a password prompt. If your host runs PHP in CGI mode, you may also need to add this line:
RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [E=HTTP_AUTHORIZATION:%1]
Without this line, you enter the correct password and still get a 401. The reason is that the web server doesn't pass the Authorization header to PHP, and your script thinks no one has logged in.
Why folder password protection works for a staging environment but not for real content
For a staging version, this method is the best choice. Fast, no code changes, and framework-independent. But for protecting the real content of your site, it's the wrong choice. There are three reasons.
- Basic Auth credentials are transmitted over HTTP as Base64; that is, unencrypted. Without HTTPS, anyone on the network path can read the password.
- There's no way to log out. The browser keeps the password until you close the window. On a shared computer, that means the next person gets into your panel.
- User management is hard. For each person you have to add a new line to the file, and to remove one you have to edit the file by hand.
If you want real protection, application-level authentication with sessions and secure cookies, or restricting access by IP, is the better choice. Keep folder password protection for "temporarily closing a path," not for "permanent security."
The effect of folder password protection on crawlers and Google indexing
This part is misunderstood by most site admins. When a folder is protected with Basic Auth, Googlebot gets a 401 Unauthorized response. This response means "you don't have access," not "I don't exist." The difference between these two is the whole story.
The practical result: the page is not removed from the index. If that URL was previously indexed and appears in search results, it stays in the results after enabling the password too; the user just lands on a password page when clicking it. This is the worst case: traffic comes in, the user sees nothing, and the bounce rate goes up.
If the goal is removal from the index, folder password protection is the wrong tool. You should return a 410 Gone response or use the noindex tag. If the goal is only a temporary closure that will be reopened later, a 401 doesn't cause a problem.
Another point: if the protected folder is inside the main site path and a link from other pages points to it, Googlebot follows that link and gets a 401. This isn't a penalty in itself, but if there are many internal links to 401 paths, the site's crawl budget is wasted. Before applying the password, remove the internal links to that path.
Here's where they go wrong: the HTTPS redirect getting locked
The most common scenario I see in tickets is this: the site admin applies folder password protection to the main folder to "lock the whole site." Then they also put the forced HTTPS redirect in the same .htaccess. The result is an endless loop. The browser says "too many redirects" and the site won't open in any browser.
The technical cause: the order in which rules execute in .htaccess matters. If the AuthType block comes before the RewriteRule rules, the first HTTP request hits the password page, then gets redirected, and the same thing happens again on the next request. The solution is to do the HTTPS redirect in the <VirtualHost> block or at the domain level, not inside a folder that has a password. If you're on shared hosting and don't have access to VirtualHost, put the redirect in the main folder and apply the password only to subfolders.
The sign of this error is simple: with curl -I https://example.com you get 301 several times in a row, and the destination address points back to itself each time. If you see this, first comment out the password block so the site opens, then fix the order.
Folder password protection on Nginx
Nginx doesn't recognize the .htaccess file. If your site is on Nginx and you create an .htaccess file, nothing happens and you think the password isn't working. You need to write this in the server or location block:
location /staging/ {
auth_basic "Staging Area";
auth_basic_user_file /etc/nginx/.htpasswd;
}
The password file is created with the same htpasswd, only its path is different. If you're on Linux shared hosting and have a control panel, usually the "Password Protect Directories" option in cPanel does this for you and there's no need for manual editing. For high-traffic sites where you need full control over the web server, a dedicated server gives you the freedom to both configure Nginx and apply the rules at the right level.
Before applying the password, check these three things
- The
AuthUserFilepath should be absolute and outsidepublic_html. Confirm it exists withls -la /home/USERNAME/.htpasswd. - Forced HTTPS should be set up correctly. The guide installing SSL on hosting and forcing HTTPS without a redirect loop explains exactly this trap.
- No internal links to the protected path should remain. Find them with
grep -r "/staging" public_html/.
If you've just brought your site up and aren't sure the folder structure is right, the guide uploading your site to hosting is a better starting point so you don't have to fix the paths by hand later.
One practical note about large files: if the folder you're password-protecting contains archives or large backup files, each password request reads the entire file from disk once. On shared plans with slow disks, this means high TTFB. Move backup files out of public_html entirely, rather than password-protecting them. To quickly check response speed, use the free webmaster tools.
And if you're on shared hosting and want to know what features you have available, Linux hosting is an option that has both cPanel and filesystem access for creating the password file.
Frequently asked questions
Does folder password protection remove a page from Google?
No. Googlebot gets a 401 response, which means "I don't have access," not "the page doesn't exist." The page stays in the index, and the user lands on the password page when clicking the result. For real removal you should return a 410 response or set the noindex tag.
Why do I get a 500 error after password-protecting a folder?
It's almost always because of the AuthUserFile path. If you write a relative path or the password file doesn't exist, Apache returns a 500 error, not a 401. Write the path as absolute and confirm the file exists with ls.
Why doesn't folder password protection work on Nginx?
Because Nginx doesn't read the .htaccess file. You need to write the auth_basic directive in the location block inside the Nginx config. If you don't have access to the config, use the Password Protect option in the control panel.
Can I password-protect just one file instead of the whole folder?
Yes. Instead of putting .htaccess in the folder, you can use the <Files "secret.php"> block and write the password rules inside it. This method comes in handy when the rest of the files in the same folder should remain public.