Access Control Using .htaccess Files

Updated November 2026 for Apache 2.4. The complete, current reference is Access Control in Apache 2.4 in THE Ultimate Htaccess.

Access control is crucial for securing web directories and files. Apache's .htaccess files provide a flexible way to implement access restrictions, ensuring only authorized users can access certain parts of your website.

Restricting Access by IP Address

To limit access to specific IP addresses, use the following directives in your .htaccess file:

<Files "admin.php">
    Require ip 192.168.1.100
    Require ip 10.0.0.0/24
</Files>

Only the listed addresses can request admin.php. Separate Require lines are OR'd, and IPv6 and CIDR ranges work too. <Files> matches a file name, not a directory: to lock a whole directory, put the Require ip lines in that directory's own .htaccess.

This is Apache 2.4 syntax. The old Order, Allow and Deny lines are 2.2, and mixing them with Require in one file gives results nobody can predict. If your site sits behind a CDN or load balancer, Apache sees the proxy's address unless the host has configured mod_remoteip.

Password Protection

Basic authentication can be set up to protect sensitive directories:

AuthType Basic
AuthName "Restricted Area"
AuthUserFile /home/you/private/.htpasswd
Require valid-user

Create the password file with bcrypt (htpasswd -B -c /home/you/private/.htpasswd alice), keep it outside the document root, and only use Basic auth over HTTPS: the password is sent base64-encoded, not encrypted, on every request.

Blocking User Agents

To block unwanted bots or specific user agents:

SetEnvIfNoCase User-Agent "BadBot" bad_bot
<RequireAll>
    Require all granted
    Require not env bad_bot
</RequireAll>

A negative Require not cannot stand alone. It has to sit inside <RequireAll> next to something positive, like Require all granted.

Restricting Access by Referrer

To stop other sites from embedding your images (hotlinking):

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https://(www\.)?example\.com/ [NC]
RewriteRule \.(?:jpe?g|png|gif|webp|avif)$ - [F,NC]

The empty-Referer line keeps direct visits, bookmarks and privacy-conscious browsers working. The Referer is trivial to strip or fake, so treat this as bandwidth protection, not security. A version that uses the browser's Sec-Fetch-Site header instead is in the 2026 guide.

Allowing/Denying Specific File Types

To restrict access to specific file types:

<FilesMatch "\.(md|log|bak|sql|env)$">
    Require all denied
</FilesMatch>

Be careful with broad extensions: denying .txt would also block robots.txt and ads.txt.

Conclusion

Using .htaccess for access control enhances the security of your web applications by restricting access based on IP addresses, implementing password protection, blocking malicious bots, and controlling access to specific files and directories. Regularly review and update your .htaccess configurations to adapt to new security challenges and requirements.

Comments