.htaccess Generator
About the .htaccess Generator
This .htaccess generator writes Apache configuration for the jobs most sites need: forcing HTTPS, choosing www or non-www, 301 redirects, custom error pages, gzip compression, browser caching, security headers and hotlink protection. Toggle what you want and copy the result; every block is wrapped in <IfModule> so a missing module will not take your site down with a 500 error.
What is an .htaccess file?
.htaccess is a per-directory configuration file read by the Apache HTTP Server (and LiteSpeed, which understands the same syntax). Rules in it apply to the folder it sits in and every subfolder, without restarting the server.
It only works when the main server config allows it with AllowOverride, for example AllowOverride All. Nginx does not read .htaccess at all; there the same rules must be rewritten as server block directives.
Force HTTPS and www with mod_rewrite
The HTTPS option adds RewriteCond %{HTTPS} off and RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301], a permanent redirect that keeps the path and query string. Behind a load balancer or CDN that terminates TLS, Apache always sees plain HTTP, so test %{HTTP:X-Forwarded-Proto} instead or you will get a redirect loop.
Pick one hostname, www.example.com or example.com, and redirect the other to it with a 301 so links and rankings are not split across two versions of every page.
301 vs 302 redirects
A 301 says the move is permanent: browsers cache it and search engines transfer the old URL's signals to the new one. A 302 is temporary, so the old URL stays indexed. Use 302 for short-lived cases like a maintenance page or an A/B test.
Redirect sources here match the exact path, so /old-page becomes RewriteRule ^old-page$ /new-page [L,R=301]. Regex characters such as dots are escaped for you, so /about.html becomes ^about\.html$. RewriteRule never sees the query string; matching ?id=5 requires a separate RewriteCond %{QUERY_STRING} rule.
Caching, compression and security headers
With mod_expires, images, icons and fonts are cached for 1 year, CSS and JavaScript for 1 month, HTML for 0 seconds and everything else for 1 week. Long cache times are safe only when file names change on each release, for example app.3f9a1c.js.
The security block sets X-Content-Type-Options: nosniff, X-Frame-Options: SAMEORIGIN, a strict-origin-when-cross-origin referrer policy and a Permissions-Policy that turns off camera, microphone and geolocation. With HTTPS on, it also adds Strict-Transport-Security with a one-year max-age. Only enable HSTS once every subdomain works over HTTPS, because browsers will refuse plain HTTP for a year.
How to Use the .htaccess Generator
- Under Options, switch on the blocks you need: Force HTTPS, Gzip compression, Browser caching, Security headers, Disable directory listing, Block access to dotfiles and Hotlink protection.
- Choose www handling (Leave as is, Force www or Force non-www) and enter your Domain if you use hotlink protection.
- Click Add redirect for each old path, enter the new path or full URL, and pick 301 or 302.
- Fill in any Custom error pages you have, such as /404.html.
- Click Copy or Download, back up your current file, and upload the result to your site root as .htaccess. Test on staging first.
Frequently Asked Questions
- Where do I put the .htaccess file?
- In your site's document root, usually public_html or www. Rules apply to that folder and all subfolders.
- Why did my download save as "htaccess" without the dot?
- Many browsers and operating systems strip or hide leading dots in downloaded file names. Rename the file to .htaccess after uploading it.
- My site shows a 500 Internal Server Error after uploading. What now?
- Restore your backup, then check the Apache error log. Common causes are AllowOverride not permitting a directive, or a typo in hand-edited rules.
- Does .htaccess work on Nginx?
- No. Nginx ignores .htaccess. The same redirects and headers must be written as directives in the Nginx server configuration.
- Why does Force HTTPS cause a redirect loop?
- Usually because a proxy or CDN handles TLS and talks to Apache over HTTP. Replace the %{HTTPS} check with RewriteCond %{HTTP:X-Forwarded-Proto} !https.
- Is my configuration sent anywhere?
- No. The file is generated in your browser; your domain and redirects are not uploaded or saved.