htaccess Generator
Free htaccess generator: tick the rules you need, from HTTPS and www redirects to caching, compression and security headers, and copy a commented Apache 2.4 .htaccess file.
Needed for hotlink protection. When set, www rules only apply to this domain.
Redirects
One per line: old path, a space, then the new path or full URL.
Speed
Security
Downloads
Back up your current .htaccess before replacing it, and merge rules from your CMS (WordPress, Laravel) below these.
Build a Safe .htaccess File Without Memorizing Apache Syntax
The .htaccess file lets you change how an Apache web server handles your site, folder by folder, without access to the main server configuration. One typo can take the whole site down, though, and mod_rewrite syntax is hard to remember. This htaccess generator writes the rules for you.
Tick what you need: HTTPS and www redirects in a single hop, custom 301 redirects, error pages, GZIP compression, browser caching, protection for hidden files and security headers. Every module-specific block is wrapped in an IfModule check and commented, so you can see what each rule does.
How to use the htaccess generator
- Enter your domain if you want hotlink protection or www rules limited to one domain.
- Choose the redirects: HTTPS, www or non-www, trailing slashes and your own old-to-new URLs.
- Turn on speed and security options and pick cache lifetimes for each file type.
- Read the warnings on the right, then copy or download the file.
- Back up your current .htaccess, upload the new one to the folder it should apply to (usually the web root) and test the site, including a few old URLs.
What each rule does
| Rule | Apache module | Effect |
|---|---|---|
RewriteRule … [R=301] | mod_rewrite | Permanent redirect; search engines move rankings to the new URL |
ErrorDocument 404 | core | Shows your own page for missing URLs |
Options -Indexes | core | Returns 403 instead of listing folder contents |
AddOutputFilterByType DEFLATE | mod_deflate + mod_filter | GZIP-compresses text files, often 60–80% smaller |
ExpiresByType | mod_expires | Sets Expires and Cache-Control max-age per file type |
Require all denied | mod_authz_core | Blocks direct access to matching files |
Header always set | mod_headers | Adds security headers such as HSTS and nosniff |
Content-Disposition: attachment | mod_headers | Makes the browser download a file instead of opening it |
How the redirects are written
When HTTPS and a www preference are both selected, the generator combines them into one rule set, so http://www.example.com/page goes straight to https://example.com/page in one redirect instead of two. The host name is captured with %1, which means the same file works on any domain unless you enter yours. Query strings are kept automatically.
Custom redirects become exact-match RewriteRule lines: /old-page /new-page turns into RewriteRule ^old-page/?$ /new-page [R=301,L], with special characters escaped and an optional trailing slash. Unlike the simpler Redirect directive, this does not also redirect everything that starts with the old path. A percent sign in the target is escaped and the NE flag added so encoded URLs stay intact.
Tips for a working .htaccess
- Got a 500 error? The server does not allow one of the directives. Hosts set this with
AllowOverride;Optionslines are the most common cause. Remove that block or ask your host. - Test redirects with 302 first if you are unsure. Browsers cache 301 redirects hard, so a mistake can stick until the cache is cleared.
- Behind Cloudflare or a load balancer, tick the proxy option. Otherwise the server sees every request as plain HTTP and the HTTPS rule loops forever.
- Start HSTS with a short max-age, then raise it to a year once every subdomain works over HTTPS. Only add preload when you are sure.
- Keep CMS rules. WordPress and Laravel ship their own rewrite block for pretty URLs; keep it below the generated rules.
- Cache long, rename often. One-year caching for CSS, JavaScript and fonts works best with versioned file names such as
app.3f2a1c.css. Shrink images first with the image compressor. - Set the matching tags in your HTML: canonical URLs from the meta tag generator should use the same HTTPS and www version as your redirects.
Frequently Asked Questions
Common questions about .htaccess files.
It is a configuration file for the Apache web server that applies to the folder it is in and all subfolders. It can redirect URLs, set headers, control caching and restrict access without editing the main server configuration.
Upload it to the document root of your site, often called public_html, www or public. A file in a subfolder only affects that folder.
No. Nginx ignores .htaccess files and needs its own server block configuration. LiteSpeed and OpenLiteSpeed read most Apache .htaccess rules.
Use RewriteCond %{HTTPS} off followed by RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]. Tick Redirect HTTP to HTTPS above to get it inside the correct IfModule block.
It only applies the rules inside it when that Apache module is loaded. That prevents a 500 error if, for example, mod_headers is not installed, but it also means the rules silently do nothing in that case.
Either works for search engines. Pick one, redirect the other to it with a 301 and use the same version in your canonical tags and sitemap.
Usually HTTPS is handled by a proxy or CDN, so Apache never sees HTTPS as on. Tick the proxy option, or check that your CDN connects to the server over HTTPS.
A little. Apache reads every .htaccess file on the path for each request. On your own server you can move the rules into the main configuration and set AllowOverride None for the best speed.