Enhancing Web Security with .htaccess Files

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

Securing a web server is crucial to protect sensitive data and ensure the integrity of web applications. Apache’s .htaccess files offer a powerful way to implement various security enhancements. Here are several key techniques:

1. Restrict Access by IP

Limit access to certain parts of your site by IP address.

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

This restricts admin.php to a single IP address. To lock a whole directory, put Require ip 192.168.1.100 in that directory's own .htaccess. Use Require, not the Apache 2.2 Order/Deny/Allow lines.

2. Disable Directory Browsing

Prevent users from viewing a list of files in directories without an index file.

Options -Indexes

3. Protect Sensitive Files

Deny access to configuration files, backups, dumps and editor leftovers. Forgotten files like these are behind a depressing number of breaches.

<FilesMatch "(^\.ht|^(config|db\.inc)\.php$|\.(bak|old|orig|swp|sql|env|log|ini)$)">
    Require all denied
</FilesMatch>

4. Prevent Script Execution

Stop scripts from running in upload directories. <Directory> cannot be used in .htaccess, so save this as .htaccess inside the uploads directory itself:

<FilesMatch "\.(php\d?|phtml|phar|pl|py|cgi|sh)$">
    Require all denied
</FilesMatch>
Options -ExecCGI

5. Set Secure Headers

Improve security by setting various HTTP headers.

Header always set X-Content-Type-Options "nosniff"
Header always set X-Frame-Options "SAMEORIGIN"
Header always set Referrer-Policy "strict-origin-when-cross-origin"
Header always set Permissions-Policy "camera=(), microphone=(), geolocation=()"
Header always set Content-Security-Policy-Report-Only "default-src 'self'; frame-ancestors 'self'"

always makes the headers go out on error pages and redirects too. X-XSS-Protection is gone from this list on purpose: browsers removed the filter it controlled. Start Content-Security-Policy in Report-Only mode, fix what it reports, then switch to the enforcing header.

6. Enable HTTPS

Force HTTPS to ensure encrypted communication.

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

<If "%{HTTPS} == 'on'">
    Header always set Strict-Transport-Security "max-age=300"
</If>

HSTS tells browsers to never try plain HTTP again. Start with a short max-age, confirm nothing broke, then raise it to 31536000; it is hard to take back. Behind a CDN or load balancer, test %{HTTP:X-Forwarded-Proto} instead of %{HTTPS} or you will get a redirect loop.

7. Password Protection

Add basic authentication to protect directories. Keep the .htpasswd file outside the document root, create it with bcrypt (htpasswd -B), and only use it over HTTPS.

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

Conclusion

Using .htaccess files to implement these security measures helps protect your web server against common vulnerabilities and unauthorized access. Regularly review and update your .htaccess rules to maintain robust security for your web applications.

Comments