$devtoolkit.sh/examples/security/htaccess-rules

Generate Apache .htaccess Security Rules

Apache .htaccess files control URL rewriting, access control, security headers, and caching at the directory level without restarting the server. This example includes HTTPS redirection, the most critical security headers, directory listing prevention, and a file type block for sensitive extensions. The generator creates rules compatible with Apache 2.4 and validates the RewriteRule syntax. Always test .htaccess changes in a staging environment as a single syntax error takes down the entire virtual host.

Example
# Force HTTPS
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Security Headers
Header always set X-Content-Type-Options "nosniff"
Header always set X-Frame-Options "DENY"
Header always set X-XSS-Protection "1; mode=block"
Header always set Referrer-Policy "strict-origin-when-cross-origin"

# Prevent directory listing
Options -Indexes

# Block access to sensitive files
<FilesMatch ".(env|log|bak|sql|conf)$">
  Order allow,deny
  Deny from all
</FilesMatch>
[ open in .htaccess Generator → ]

FAQ

Does .htaccess work on Nginx servers?
No. .htaccess is an Apache-specific feature. Nginx does not support it; configuration must be placed directly in the nginx.conf or server block files.
Can a single .htaccess error bring down my site?
Yes. Apache returns a 500 Internal Server Error for any .htaccess syntax mistake, taking down all pages in that directory. Always validate syntax with the generator and test on staging first.
Where should I place .htaccess files?
Place the primary file in your document root (public_html or www) for site-wide rules. You can place additional .htaccess files in subdirectories for directory-specific overrides.

Related Examples

/examples/security/htaccess-rulesv1.0.0