Developer Tools· 6 min read

Generate Apache .htaccess Rules for HTTPS, Caching, and More

Toggle common Apache directives and download a ready-to-use .htaccess file.

By EasyDevTools Team Last updated: 2026-08-24

Why .htaccess configuration gets it wrong so often

The `.htaccess` file is Apache's distributed configuration mechanism — a single text file placed in a directory that overrides server-level settings for that directory and all subdirectories. It is the go-to solution for enforcing HTTPS, setting up redirects, configuring caching headers, enabling gzip compression, and blocking hotlinking. Despite its importance, most developers build their `.htaccess` files by copying snippets from Stack Overflow, which leads to conflicting rules, redundant directives, and subtle syntax errors.

A single misplaced `RewriteRule` can create an infinite redirect loop that takes down a site. An incorrectly ordered `mod_deflate` directive might compress files that should not be compressed, breaking binary downloads. A `mod_expires` rule with an overly long cache time can prevent visitors from seeing updated content for days. These problems are hard to debug because `.htaccess` errors often produce generic 500 Internal Server Error pages with no specific details.

A structured generator that lets you toggle each feature on and off, enter your domain and paths, and see the resulting `.htaccess` code live eliminates the guesswork. You get syntactically correct rules, properly ordered directives, and a file you can copy or download directly to your server.

See it in action

Directives you can configure

DirectiveWhat It DoesApache Module
HTTPS redirectForces all HTTP traffic to HTTPSmod_rewrite
WWW redirectRedirects example.com to www.example.commod_rewrite
Custom 404 pageServes your own not-found pageErrorDocument
Gzip compressionCompresses text-based assets for faster transfermod_deflate
Browser cachingSets Cache-Control and Expires headersmod_expires
Disable directory listingHides file indexes for directories without index.htmlOptions -Indexes
Hotlink protectionBlocks other sites from embedding your imagesmod_rewrite + Referer

How to generate your .htaccess file

Enter your domain name (e.g., example.com) in the domain field. This is used by the HTTPS redirect, WWW redirect, and hotlink protection rules to match your site's URLs.

Enter the path to your custom 404 error page (e.g., /404.html) if you want to serve a branded not-found page instead of Apache's default.

Toggle each directive on or off using the switches: HTTPS redirect, WWW redirect, gzip compression, browser caching, directory listing disable, and hotlink protection.

Review the generated `.htaccess` code in the live preview panel. The code updates instantly as you toggle each option.

Click Copy to copy the code to your clipboard, or click Download to save it as a `.htaccess` file that you can upload directly to your server's document root.

Testing your .htaccess rules before deploying

Before uploading the generated file to a production server, test it on a staging environment or a local Apache installation. Create a test directory with a few HTML files, place the `.htaccess` file in it, and verify each rule one at a time. Enable HTTPS redirect first and confirm that `http://` requests are redirected to `https://`. Then enable WWW redirect and confirm the non-WWW version redirects correctly. Add gzip and caching last, since these are less likely to cause visible errors.

Use your browser's developer tools Network tab to confirm that gzip compression is working (check the `Content-Encoding: gzip` response header) and that caching headers are present (`Cache-Control`, `Expires`). If you enable hotlink protection, test it by embedding one of your images on a different domain and confirming the image is blocked. This methodical approach catches configuration issues before they affect real users.

Common mistakes in .htaccess configuration

Placing the file in the wrong directory: `.htaccess` affects the directory it is placed in and all subdirectories. If you put it in a subdirectory, rules will not apply to the parent or sibling directories. For site-wide rules, place it in the document root.

Enabling both HTTPS and WWW redirects in the wrong order: the HTTPS redirect must fire before the WWW redirect, otherwise you may redirect from HTTP non-WWW to HTTPS non-WWW and then to HTTPS WWW, creating an extra hop. The generator orders these correctly.

Forgetting that `.htaccess` is Apache-only: these rules use `mod_rewrite`, `mod_deflate`, and `mod_expires`, which are Apache modules. They will not work on nginx, Caddy, or IIS.

Setting cache times too aggressively: a 1-year cache on HTML files means visitors will not see content updates without a hard refresh. Use short cache times for HTML (minutes to hours) and long times for static assets like images and fonts (weeks to months).

Edge cases to watch for

If your site is behind a CDN or reverse proxy like Cloudflare, the HTTPS redirect in `.htaccess` may be redundant or even counterproductive. The CDN typically handles HTTPS termination and redirects before the request reaches your origin server. In this case, disable the HTTPS redirect toggle and let the CDN manage it.

Subdirectory installations (e.g., WordPress in `/blog/`) can complicate `.htaccess` rules because the base path differs from the domain root. If your application lives in a subdirectory, the generated rules may need adjustment to the `RewriteBase` directive. The generator produces rules for the domain root by default, so review and modify if your setup is different.

Real-world use cases

Launching a new website and needing a complete `.htaccess` file that enforces HTTPS, standardizes the WWW prefix, enables compression for faster page loads, and sets appropriate browser caching headers for static assets.

Securing a WordPress site by adding hotlink protection to prevent other blogs from embedding your images (which consumes your bandwidth without driving traffic to your site), along with a custom 404 page that guides lost visitors.

Migrating a site from HTTP to HTTPS and needing a reliable 301 redirect rule that passes link equity to the HTTPS URLs without creating redirect chains.

Setting up browser caching headers on a static site hosted on shared Apache hosting where you do not have access to the main server configuration file.

Frequently asked questions

Q: Will these rules work on nginx?

A: No — `.htaccess` is Apache-specific. It relies on Apache modules like mod_rewrite, mod_deflate, and mod_expires. For nginx, use a dedicated nginx config generator.


Q: Does the HTTPS redirect use a 301 or 302 status code?

A: It uses a 301 (permanent) redirect, which search engines respect for link equity transfer. Use a 302 only if the redirect is temporary.


Q: What is hotlink protection and how does it work?

A: Hotlink protection blocks other websites from embedding your images by checking the HTTP Referer header. If the referer does not match your domain, the request is denied with a 403 Forbidden response.


Q: Can I combine this with my existing .htaccess rules?

A: Yes — paste the generated rules into your existing file, but be careful about duplicate directives. If you already have `RewriteEngine On`, do not add it again.


Q: What if I get a 500 Internal Server Error after uploading?

A: This usually means a syntax error in the file. Re-download from the generator, check for any manual edits you made, and ensure the file uses Unix line endings (LF) rather than Windows (CRLF).


Q: Is my configuration data uploaded?

A: No. The `.htaccess` file is generated locally in your browser.

Generate your .htaccess now

Use the .htaccess Generator to build a production-ready Apache configuration file in seconds. For related developer tools, see nginx Config Generator, .gitignore Generator, or CSP Builder.

Need help using this tool?

Read our complete .htaccess Generator tutorial for step-by-step guidance.

Ready to try the tool?

No accounts. No uploads. No limits. Start now.