Biggest deals of the year. 30-day money-back guarantee. Limited time deals. 24/7 support. Don't miss out

How to restrict website access using .htaccess

Nov 06, 2026 3 min Read
How to restrict website access using .htaccess

You can restrict access to your website using .htaccess rules that block specific IP addresses or permit only trusted visitors. This is useful when testing a website, limiting access to a development environment, or blocking unwanted traffic.

In this tutorial, you’ll learn how to edit the .htaccess file, configure IP-based restrictions, and verify that your access rules work without locking you out of your website.

Before restricting access with .htaccess

The .htaccess file controls certain web server settings, including redirects and access restrictions. These instructions use Apache 2.4 access-control syntax, so make sure your hosting environment supports the relevant directives.

Before editing the file, create a backup of your existing .htaccess configuration. Incorrect rules can block legitimate visitors or cause server errors.

You’ll also need the public IP address you want to permit or block. If your IP address changes frequently, consider using password protection instead of relying on an IP-based restriction.

How to edit the .htaccess file

If you want to edit the .htaccess file, you should enter the root folder of your site through your hosting control panel’s file manager or an FTP client such as FileZilla.

To edit .htaccess using Hostinger File Manager:

  1. Log in to your Hostinger account and open Websites.
  2. Find your website and click Manage site.
  3. Navigate to Files → File Manager.
  4. Open the public_html directory and locate .htaccess.
  5. Create a backup copy, then open the original file in the editor.

If .htaccess doesn’t exist, create a new file with that name. Don’t overwrite an existing file, as it may contain important WordPress or website configuration rules.

Save your changes. If you’re using the newer Hostinger File Manager, click Deploy to apply them to the live website.

You can start modifying the file right away. If you can’t find the .htaccess file in the public_html directory, you can create it yourself. Just click the New File button at the top right corner of the file manager, name it .htaccess, then click Create.

How to restrict website access by IP using .htaccess

You can configure .htaccess to permit only specific visitors, block individual IP addresses, or deny access to everyone.

The following examples use Apache 2.4 Require directives instead of the older Order, Allow, and Deny syntax.

Allow access only from your IP address

To block everyone except one IP address, add:

Require ip 203.0.113.10

Replace 203.0.113.10 with your public IP address. Only requests from the specified IP address will be permitted.

Block a specific IP address

To block one IP address while permitting other visitors, use:

<RequireAll>
    Require all granted
    Require not ip 203.0.113.10
</RequireAll>

Replace the example address with the IP you want to block.

Block access for everyone

To deny all website requests, use:

Require all denied

This blocks your access through the browser as well. Make sure you can still edit or restore .htaccess using your hosting control panel before applying this restriction.

Add only the rules needed for your access-control goal. Don’t combine these examples without checking how the authorization directives interact.

How to test your .htaccess access restrictions

After saving your changes, open your website from an IP address that should have access and one that should be blocked.

Blocked visitors should receive a 403 Forbidden response. If you’re testing an IP whitelist, confirm that your permitted IP can still access the website.

If you receive a 500 Internal Server Error, your web server may not support the directive or the configuration may contain a syntax error.

Restore your backed-up .htaccess file if the restriction behaves unexpectedly. If you use a CDN or reverse proxy, verify that the server identifies visitors’ real IP addresses before relying on IP-based rules.

Next steps: Keep your website access secure

Once you’ve confirmed that your access restrictions work, review them whenever your trusted IP addresses change.

Remove temporary restrictions when your website is ready for visitors. If you need ongoing protection for private content, consider password-based access rather than depending entirely on IP addresses.

Keep a backup of your working .htaccess configuration so you can restore access if a future change causes problems.

This article is a localized version of How to restrict website access using .htaccess, originally published in English.

All of the tutorial content on this website is subject to Hostinger's rigorous editorial standards and values.

Edvinas is a Backend Developer at Hostinger. During his free time, he likes to play Airsoft and play music with his band.

Trusted by 4+ million website owners worldwide

See our 74,512 reviews on Trustpilot