Back to Tools

.htaccess & .htpasswd Generator

Generate optimized Apache configuration files for security and performance

Instant Setup

Common Redirects

Basic Security

Folder Protection (.htpasswd)

Security Headers

Performance & Caching

Custom Error Pages

Custom Rules

Generated .htaccess

# .htaccess generated with Htaccess Generator
# .htpasswd generated with Htaccess Generator

Complete Guide to .htaccess Configuration

What is .htaccess?

The .htaccess file (Hypertext Access) is a powerful Apache web server configuration file that allows directory-level customization without modifying the main server configuration. It controls everything from URL redirects and access control to caching and security headers.

Unlike global server config (httpd.conf), .htaccess files are processed per-request and take effect immediately without server restart—making them ideal for shared hosting environments.

Key Use Cases

Security Hardening

Block directory listings, protect sensitive files, add security headers (HSTS, X-Frame-Options), and hide server version information.

URL Redirects & Rewrites

Implement 301 redirects for SEO, force HTTPS/www, create clean URLs, and manage site migrations.

Password Protection

Restrict access to staging sites or admin areas using HTTP Basic Authentication (.htpasswd).

Performance Optimization

Enable GZIP compression, set browser cache headers (Expires), and improve Core Web Vitals.

Essential Security Configurations

  • Disable Directory Indexing: Options -Indexes prevents file listing exposure.
  • Protect .htaccess: Deny direct access to configuration files.
  • X-Frame-Options: Prevents clickjacking attacks.
  • X-Content-Type-Options: nosniff stops MIME-type guessing.
  • HSTS: Forces HTTPS for all future visits (use carefully).

Performance Tips

  1. GZIP Compression: Reduces file sizes by 60-80% for HTML, CSS, JS.
  2. Expires Headers: Set far-future expiration for static assets.
  3. Minimize Overuse: Each rule adds per-request processing.

Understanding .htpasswd

The .htpasswd file stores encrypted passwords for HTTP Basic Auth:

  • Encryption: Passwords are hashed (bcrypt, MD5). Never plain text.
  • Placement: Store ABOVE public_html for security.
  • Absolute Paths: AuthUserFile requires full server path.

Common Mistakes

  • Infinite Redirect Loops: HTTPS + www redirects must check current state.
  • Forgetting RewriteEngine On: All mod_rewrite rules require this.
  • Syntax Errors: Any typo causes 500 Internal Server Error.

Frequently Asked Questions

Does .htaccess work on all servers?

No. .htaccess is specific to the Apache HTTP Server. Nginx, IIS, and Caddy use entirely different configuration mechanisms. On Nginx, equivalent rules are written in the main server configuration file (nginx.conf) using location blocks and rewrite directives. Most shared hosting providers use Apache, but VPS and cloud servers often use Nginx. Always check with your provider before uploading an .htaccess file.

Why do I get 500 errors after editing .htaccess?

A 500 Internal Server Error after saving an .htaccess file almost always means a syntax error. Common culprits include forgetting RewriteEngine On before RewriteRule directives, typos in directives, or unsupported module directives (e.g., using mod_headers directives when the module isn't loaded). Check your server's Apache error log for the exact error message. Always keep a backup copy before making changes.

Should I enable HSTS?

Only if you are 100% committed to serving your website exclusively over HTTPS, both now and in the future. HSTS (HTTP Strict-Transport-Security) instructs browsers to only connect via HTTPS for a specified duration (e.g., max-age=31536000 = 1 year). Once a browser has received the header, users cannot access the HTTP version of your site for that duration, even if you remove the header. Start with a short max-age (e.g., 300 seconds) for testing before deploying a year-long value.

How do I test changes safely?

Always test on a staging subdomain or a local development environment first. Before overwriting your live .htaccess file, download and save a copy of the original. After uploading the new file, test critical pages in an incognito browser window (to avoid cached redirects), check your admin/login pages, and verify that HTTPS redirects are working correctly without creating an infinite loop. Tools like htaccess.madewithlove.com let you simulate rules before deploying them.

About the Author: Nicola Selenu

Nicola is a Lead Developer and Digital Marketing Technologist with years of hands-on experience in technical SEO, web analytics, and deliverability. He builds tools to bridge the gap between complex marketing technicalities and easy-to-use solutions.