10 .htaccess Rules You Need: HTTPS, www, 301s and Caching
An htaccess generator is useful when you want a file you can read, not a black-box “SEO plugin.” FlashKit’s .htaccess file generator runs in this tab. Tick Force HTTPS, a www policy, hotlink protection, caching, gzip, directory listing, sensitive-file blocks, and custom error pages. Copy the output. It does not log into Apache. It does not replace apachectl configtest. Password protection is a different problem — .htpasswd on Apache and Nginx — and this page does not repeat it. To create .htpasswd users from the command line, use htpasswd -B.
Every rule below was loaded on Apache 2.4.58 (Ubuntu), built 2026-10-16, with mod_rewrite and mod_expires, and probed with curl -I on 10 Nov 2026. .htaccess only applies when the parent <Directory> has AllowOverride covering FileInfo (rewrites, ErrorDocument) and Indexes. Nginx, Caddy, Netlify, and Vercel ignore the file.
Generate the Apache file in this tab
HTTPS+www is one hop. Hotlink dots are escaped. Cache uses image/jpeg. Review before you deploy.
Open the htaccess generatorRules with explanation
FlashKit prefixes the file with # Apache .htaccess generated by RAKKOTOOLS and wraps rewrite in <IfModule mod_rewrite.c> with RewriteEngine On. The snippets below are that generator’s output, or the extra 301 used in the lab for a path rename.
1. Force HTTPS only
When www redirect is “No Redirect”, the generator emits:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
That is an htaccess redirect http to https that keeps the current Host. It does not add or strip www.
2. HTTPS and www in one hop
Ticking Force HTTPS and “Non-WWW to WWW” together is the combined rule, not two RewriteRules. Lab vhost on port 18080, curl -I -H 'Host: example.com' http://127.0.0.1:18080/:
HTTP/1.1 301 Moved Permanently
Server: Apache/2.4.58 (Ubuntu)
Location: https://www.example.com/
The same Location for Host: www.example.com over HTTP. One hop. The generator writes:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,R=301]
Apache mod_rewrite documents [L] (last) and [R=301]. Two sequential rules (http → https, then apex → www) would cost a second round-trip and split link equity across two 301s. Do not do that.
3. HTTPS and strip www in one hop
The “WWW to Non-WWW” twin, lab port 18082. Host: www.example.com over HTTP returned Location: https://example.com/ in one 301. So did Host: example.com over HTTP. Generator output:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]
4. Path 301: /old.html → /new.html
Not a generator toggle. Lab rule RewriteRule ^old\.html$ /new.html [R=301,L]. curl -I http://127.0.0.1:18081/old.html with Host: example.com:
HTTP/1.1 301 Moved Permanently
Location: http://example.com/new.html
Put specific path rules after the canonical-host rule if you also force HTTPS, so the public Location is already on the final scheme and host. This lab vhost was HTTP-only, so Location stayed http://.
5. Hotlink protection, every dot escaped
Domain field example.co.uk. The generator escapes every . (example\.co\.uk), not only the first. Rule:
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?example\.co\.uk [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]
Against /img/photo.jpg on 18081: Referer https://evil.example/ → HTTP/1.1 403 Forbidden. Referer https://example.co.uk/ → HTTP/1.1 200 OK, Content-Type: image/jpeg, Cache-Control: max-age=31536000. Empty Referer is allowed on purpose (!^$) so privacy-stripping browsers still show images. The rule does not cover WebP or SVG.
6–8. Block .env, wp-config.php, php.ini
Generator FilesMatch (Apache 2.2-style Order allow,deny / Deny from all, still accepted on 2.4):
<FilesMatch "^(wp-config\.php|php\.ini|\.env|\.git)">
Order allow,deny
Deny from all
</FilesMatch>
On 18081, GET /.env, /wp-config.php, and /php.ini each returned HTTP/1.1 403 Forbidden. .git is in the same regex (a file named .git, not a directory listing of .git/ — directory blocking needs a separate Location or Redirect). Password files belong outside the web root; see the htpasswd guide rather than this FilesMatch.
9. Custom 404
ErrorDocument 404 /404.html (and 500 → /500.html when that toggle is on). Requesting /does-not-exist.html on 18081 returned HTTP/1.1 404 Not Found with body not-found-page (the lab’s /404.html, 15 bytes). Confirm the 404 document itself is not rewritten into a loop.
10. Browser caching with image/jpeg
Generator mod_expires block, matching mod_expires ExpiresByType:
ExpiresActive On
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/javascript "access 1 month"
ExpiresByType application/javascript "access 1 month"
ExpiresDefault "access 2 days"
/img/photo.jpg on 18081: Cache-Control: max-age=31536000 (1 year) and Content-Type: image/jpeg. /app.css: HTTP/1.1 200 OK, Cache-Control: max-age=2592000 (1 month), Content-Type: text/css. There is no image/jpg line; that type is not registered.
Two more generator toggles, tested, that sit outside the “10” headline but ship in the same file:
- Options -Indexes. Requesting
/img/on 18081 returned the custom 404 document, not an Apache file listing. Do not treat that as a substitute for locking the directory. - mod_deflate.
AddOutputFilterByType DEFLATEfor HTML, CSS, JS. A HEAD/GET of an 8-byte HTML file on 18081 withAccept-Encoding: gzipdid not setContent-Encoding: gzip. Apache’s deflate filter skips tiny bodies. Do not claim gzip from a file that small.
Order matters
FlashKit writes rewrite first (canonical host, then hotlink), then Options -Indexes, then FilesMatch, then deflate, then expires, then ErrorDocument. That order is the one to keep if you paste by hand. Canonical-host 301s before path 301s. Hotlink [F,L] after host canonicalization so a 403 is not then redirected. Expires headers are independent of rewrite; they still apply on 200. A 301 should not carry a year-long image cache — the lab’s default ExpiresDefault "access 2 days" did attach Cache-Control: max-age=172800 to some 301s. If that bothers you, move Expires into a <FilesMatch> for static types only.
Testing with curl -I
Send the Host header your vhost expects. Follow nothing on the first request (-I is HEAD). Read Location once. Then request that URL. Two Locations means two hops — fix the rules, do not “wait for Google.” Useful probes from this lab:
curl -sI -H 'Host: example.com' http://127.0.0.1:18080/
curl -sI -H 'Host: www.example.com' http://127.0.0.1:18082/
curl -sI -H 'Host: example.com' -H 'Referer: https://evil.example/' \
http://127.0.0.1:18081/img/photo.jpg
FlashKit also has an HTTP header checker if you would rather not use curl. Staging first. Keep SSH. A broken rewrite loop is a site outage, not a ranking tweak. After HTTPS works, convert leftover HTTP bookmarks with a single 301, not a chain.
AllowOverride must include FileInfo for RewriteRule and ErrorDocument, and Indexes for Options -Indexes. A host that sets AllowOverride None will ignore the entire file; apachectl -S and the vhost <Directory> tell you that, not the generator. Apache 2.4 prefers Require all denied inside FilesMatch; the generator still emits 2.2 Order allow,deny / Deny from all, which 2.4.58 accepted in this lab (403 on .env). If your host forbids the 2.2 syntax, swap that block by hand. Hotlink protection with an empty domain field is a regex that matches almost nothing useful — type the host you actually serve. Trailing-slash vs no-slash on the same resource is a separate 301; this generator does not emit those. Netlify users: FlashKit’s own trailing-slash rules live in _redirects and netlify.toml, not in .htaccess.
Need Basic Auth instead?
That is .htpasswd plus Require valid-user. The file generator here does not emit AuthType.
Open the htpasswd generator