Only Allow one IP Address on Website (.htaccess)

Intermediate Level - Badge

Requirements: This is for people using the Apache web server who have a basic understanding of coding or configuring a system.

Overview

Sometimes it’s useful to only allow a single IP address to access a website. I use this when I’m developing a website that has only just been mapped to a domain. I still want to test the website myself to make sure it’s secure before making it live to everyone.

If you know the IP address you want to block then continue to step 2. Step 1 shows you how to get your current IP address.

What is an IP Address?

Tutorial

1. Get Your IP Address

I simply type what is my ip? in to Google search. Or you can achieve the same by clicking the link below:

https://www.google.co.uk/search?q=what+is+my+ip

Google tells me that my IP address  is 111.22.33.44.

2. Only Allow Your IP Address

The easiest way to block everyone except yourself is to add the follow code to the top of your .htaccess file (what is a .htaccess file?)/ Replace 111.22.33.44 with your IP address.

Now you can use the website without other people accessing it.

Note: Apache’s .htaccess file is space-sensitive. So don’t add extra spaces in, for example after the comma. It should be order deny,allow and not order deny, allow.

IMPORTANT: Remember to remove those lines from your .htaccess file later on if you want others to be able to visit your website!

Dynamic Versus Static IP Addresses

An important caveat needs to be made about IP addresses. Most Internet Service Providers (ISPs) e.g. BT, Talk Talk, Virgin etc, will give you a dynamic IP address by default. As the name suggests this means that your IP address can potentially change sometimes. It probably won’t change that often but it can happen. So obviously the above method will only work while your IP address remains the same.

You either need to update the .htaccess file to reflect this change, OR you can give yourself a static IP address.

Getting a Static IP Address

The method I use is to sign up a static IP service from a company like NoIP (note I don’t get payment for recommending any of these). At time of writing they provide a free service.

The other method is to buy a static IP address from your ISP.

Leave a Reply

Your email address will not be published. Required fields are marked *