htaccess

How to Deny Access to Website Using .htaccess

Introduction

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 http://www.whatismyipaddress.com 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.

Conclusion

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.

Add Comment

Click here to post a comment

Join thousands of subscribers worldwide

and get awesome deals & the newest tutorials to your email

Please wait...

Thank you for sign up!