Member-only story

How to Secure and Restrict Access to Your Website Like a Pro

Katherine Petalio-Amar
2 min readOct 31, 2022

--

Photo by Patrick Robert Doyle on Unsplash

Ever wanted to restrict access to your website based on specific IP addresses?

Maybe you’re building a service that should only be accessible to certain users, or you want to block unwanted traffic. Either way, you’re in the right place! In this tutorial, I’ll walk you through the process of adding IP address restrictions to your website.

Prerequisites:

  • A web server with mod_rewrite enabled
  • Basic knowledge of how the .htaccess file works

What is the .htaccess File?

Before we dive in, let’s quickly go over what the .htaccess file is. It’s a configuration file used by Apache servers that controls various aspects of how the server handles requests. You can use it to enforce security, rewrite URLs, and—of course—restrict access by IP address.

Step 1: Create Your .htaccess File

In your main website folder, create a file called .htaccess (if it doesn't exist). Then, add the following code to it:

# BEGIN IP Access Restriction
<Limit GET POST PUT>
ErrorDocument 403 /403.html
order deny,allow
deny from all
allow from <your-allowed-IP-address>
</Limit>
# END IP Access Restriction

--

--

Katherine Petalio-Amar
Katherine Petalio-Amar

Written by Katherine Petalio-Amar

I'm a Software Developer with experience in Web Development and Mobile App Development and a bit knowledgeable in Server Management.

No responses yet