How to Deny Access to Website Using .htaccess

In some cases, you would like to block access to your website for everyone except you. Let it be for testing new website feature or any other reason. This tutorial shows how to deny every IP address except for yours using .htaccess file.

What you’ll need

Before you begin this guide you’ll need the following:

  • Access to your hosting account’s cPanel or ability to connect via FTP client
  • Static IP address. If you have a dynamic IP address, this solution might not work as expected

Step 1 — Opening up your .htaccess file.

.htaccess file is located in your public_html directory. You can open it up using File Manager in your cPanel or by connecting to your account via FTP Client (FileZilla, for example). The following tutorials describe both methods in detail:

Note that .htaccess file is a hidden file, therefore you would need to enable it’s visibility.

Step 2 — Checking your own IP address

You will need to know your own IP address in order to be able to visit your website yourself. In your browser open and take a note of your IP address:

Check your IP address

Step 3 — Modifying .htaccess file

Add the following lines to the top of the .htaccess file:

Order Deny,Allow
Deny from all
Allow from YourIPAdress

An example of this process is illustrated below:

Editing htaccess file

Don’t forget to save changes.

IMPORTANT! In case you get locked out of your website yourself, check whether your IP hasn’t changed. If you have a dynamic IP address, this solution may not work for you so well.


You have learned how to deny access to your website for everyone except you. This could be valuable for a number of reasons. Now that you know the basics of the process you can set up more advanced rules, for example, block visitors from specific countries or allow visitors from a particular IP range.

About the author

Tomas A.

Tomas is a senior customer support agent at Hostinger. Besides providing top-notch technical advice on a daily basis, he likes to create websites and do various coding side projects.

Add Comment

Click here to post a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Get Exclusive Content

Join thousands of webmasters who get our free newsletter with tips on how to drive more traffic and revenue to their websites!

Please wait...

Thank you for sign up!