.htaccess Generator
Generate Apache .htaccess rules for HTTPS redirects, www canonicalisation, caching, compression, and security hardening.
# No rules configured yet # Enable options on the left to generate your .htaccess file
An .htaccess generator lets you create Apache server configuration rules visually without writing a single line of Apache syntax from memory. The .htaccess file (hypertext access) is a per-directory configuration file that Apache reads on every request, allowing you to control redirects, security rules, caching, compression, and error handling at the directory level without touching the main server configuration.
Getting .htaccess rules right is notoriously difficult. Apache's mod_rewrite syntax is powerful but cryptic — a missing [L] flag, a wrong regular expression anchor, or a forgotten RewriteEngine On directive can make an entire site redirect loop or return unexpected errors. This generator handles the syntax for you: toggle the options you need, and the tool builds a correctly formatted file in real time that you can copy or download.
The generator covers the most common use cases: forcing HTTPS to encrypt all traffic, redirecting between www and non-www to consolidate your SEO link equity, enabling gzip compression to speed up page loads, setting browser caching headers to reduce repeat visitor load times, and hardening your server with security rules that block directory listing, hide sensitive files, and prevent image hotlinking. Each section is independently configurable — enable only what your project needs.
The generated .htaccess file is compatible with Apache 2.4 and uses standard modules: mod_rewrite, mod_deflate, mod_expires, and mod_headers. All directives include comments explaining what each block does, making the output easy to understand and extend.
How to Use the .htaccess Generator
- 1
Configure your redirect rules
Toggle HTTPS enforcement and choose your www preference — force www, force non-www, or leave as-is. These two settings affect how search engines and browsers resolve your domain and should match your SSL certificate.
- 2
Add security and performance options
Enable gzip compression to reduce page transfer sizes by 60–80%, set browser caching durations for static assets, and toggle security rules such as disabling directory listing and blocking access to hidden files like .env and .git.
- 3
Set custom error pages and redirects
Enter paths for 404, 403, and 500 error pages to show branded error messages instead of Apache defaults. Add custom 301 redirects for old URLs that have moved to new locations.
- 4
Copy or download the file
The right panel shows your generated .htaccess in real time. Click "Copy to Clipboard" to paste it directly, or click "Download .htaccess" to save the file with the correct hidden filename ready for upload.
- 5
Upload to your server root
Upload the .htaccess file to the root of your website (the public_html or www directory). Rules apply immediately — no server restart required. Test your redirects in a browser and check Apache error logs if something behaves unexpectedly.
Understanding .htaccess Directives
.htaccess file on every incoming request and applies the directives in order from top to bottom. The most important module is mod_rewrite, which provides RewriteEngine, RewriteCond, and RewriteRule. Rules are processed sequentially, and the [L] flag stops processing after a match. Conditions (RewriteCond) must all match before the following RewriteRule fires.
Performance directives — mod_deflate for gzip and mod_expires for caching — reduce the bytes transferred on each request and the number of requests the browser makes. Security directives prevent information disclosure: Options -Indexes hides directory listings, <FilesMatch> blocks access to sensitive files, and referer-based rules prevent other sites from hotlinking your images and consuming your bandwidth.| Field | Description |
|---|---|
| RewriteEngine On | Must appear before any RewriteRule or RewriteCond. Enables the mod_rewrite engine for the current directory. |
| RewriteCond | A condition that must be true for the following RewriteRule to fire. Multiple conditions are ANDed together by default. |
| RewriteRule | Defines the URL pattern to match and the target to rewrite or redirect to. Flags like [L,R=301] control behaviour. |
| ErrorDocument | Maps an HTTP status code to a custom page path. ErrorDocument 404 /404.html shows your branded 404 page. |
| Options -Indexes | Disables directory listing. Without this, Apache shows file lists for any directory that lacks an index.html. |
| mod_expires | Sets browser caching (Expires headers) per file type. Longer cache durations reduce bandwidth and repeat visitor load times. |
| mod_deflate | Enables gzip compression of text-based responses. Reduces transfer size by 60–80% for HTML, CSS, and JavaScript. |
Common .htaccess Use Cases
Force HTTPS on every page
The most critical redirect for any modern website. Redirecting HTTP to HTTPS ensures all traffic is encrypted and prevents the browser from showing "Not Secure" warnings. It also consolidates PageRank to the HTTPS version of your pages.
Canonicalise www vs non-www
Google treats www.example.com and example.com as separate URLs. Consistently redirecting one to the other consolidates your link equity to a single canonical domain, improving SEO and preventing duplicate content penalties.
Improve page speed with caching and compression
Browser caching tells visitors' browsers to store static assets locally for days or months, dramatically reducing load times on return visits. Combined with gzip compression, these rules can cut page weight by 70% and shave seconds off load time.
Harden server security
Block directory listing so attackers cannot see your file structure, protect the .htaccess file itself from web access, block .env and .git directories from being served, and prevent hotlinking that would drain your bandwidth allowance.