How to Password Protect a Website or a Single Page

A real password check happens on the server before the page is sent. Use your host's or site builder's page password setting, HTTP Basic authentication in Apache or Nginx, or proper member accounts. A password box written in JavaScript only hides the page in the browser; the content still arrives and anyone can read the source.

Ways to put a password on a site
OptionProtects content?Best for
Builder or CMS page passwordYes, checked by the platformOne private page for a client or event
HTTP Basic auth (Apache, Nginx)Yes, over HTTPSA staging site or a whole folder
Member accountsYes, per personPaid or long-running private areas
JavaScript prompt or hidden divNoNothing that matters

Updated

Type a username and password to see the exact Authorization header a browser sends under Basic authentication, and how easily it turns back into plain text. It shows why Basic auth must only run over HTTPS. The encoding happens on your device; nothing is sent.

What Basic auth sends

Username: client

Password: spring-preview

The username cannot contain a colon, because the colon separates it from the password.

Header the browser sends with every request

Authorization: Basic Y2xpZW50OnNwcmluZy1wcmV2aWV3

Decoded by anyone who sees the request

client:spring-preview

Base64 is not encryption. Without HTTPS, this header gives the password away.

Server setups that really protect a folder

Create the password file with the htpasswd tool that ships with Apache (for example htpasswd -c /etc/apache2/.htpasswd client), and keep it outside the public web folder.

Apache: .htaccess in the folder to protect

Require valid-user lets in anyone listed in the password file.

AuthType Basic
AuthName "Private preview"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user

Nginx: protect one location

Nginx reads the same htpasswd file format. Reload Nginx after editing.

location /preview/ {
    auth_basic           "Private preview";
    auth_basic_user_file /etc/apache2/.htpasswd;
}

What not to do: a JavaScript gate

Shown so you can recognise it. The password and the content are both in the file every visitor downloads.

<div id="secret" hidden>Client price list ...</div>
<script>
  if (prompt("Password?") === "spring-preview") {
    document.getElementById("secret").hidden = false;
  }
</script>

Why script-only gates fail

A common tutorial adds a prompt() and compares the answer with a password written in the same script. The browser has already downloaded the full page, and the password sits in the source for anyone who presses view source. Turning off JavaScript skips the check altogether.

The same goes for content hidden with CSS. If the words are in the HTML, they are public.

Choosing between the real options

For one page you share with a client, the page password in your builder or CMS is the quickest safe choice. For a staging copy of a site, Basic auth on the server keeps the whole thing private, including from search engine crawlers.

When different people need different access, or you need to remove one person later, use accounts. A single shared password cannot be taken back from one person without changing it for everyone.

How it works

  1. Decide whether one shared password is enough or each person needs their own login.
  2. On a site builder or WordPress, look for a page visibility or password setting first.
  3. On your own server, add Basic authentication to the folder or location, with the password file kept outside the public web folder.
  4. Check that the site forces HTTPS, then test in a private browser window.

Tips

Frequently asked questions

Can I password protect a page with just HTML?

No. HTML has no way to check a password. Anything you put in the file is downloaded by every visitor, so the check has to happen on the server or platform.

Is HTTP Basic authentication secure?

Only over HTTPS. RFC 7617 points out that the password crosses the network in cleartext, because Base64 is an encoding, not encryption, and says the scheme should not protect sensitive information without something like HTTPS.

How do I password protect a page in WordPress?

Open the page or post, set Visibility to Password protected, enter a password and update. Visitors see the title and a password prompt instead of the content.

Can I protect a whole WordPress site?

WordPress's own documentation says hiding the entire site is not part of core, and points to plugins or server rules such as .htaccess.

What does the browser show for Basic auth?

The server answers with 401 and a WWW-Authenticate header. The browser then asks for a username and password and repeats the request with an Authorization header.

Related: 401 vs 403, Authentication guide, Make a membership website, HTTP status codes.

Start for free or see all free tools. We.Inc is free to start, with no card needed. AI building is on paid plans from $20 a month.

More in Guides

We.Inc is an AI-powered website builder you can resell under your own brand. Launch a branded client dashboard, bill on Stripe Connect, and deliver AI-generated websites in minutes. White-label plans start at $99 a month for 25 client sites, with a 7-day free trial and no per-site fees.

Product

Who It's For

Features

Resources

Company

View Sitemap