.htaccess: HTTPS, redirects and caching explained
Reviewed 23 November 2026 · 7 min read · Written and maintained by Toolnexo
An .htaccess file is a small text file that changes how an Apache web server treats the folder it sits in. A few lines can send visitors to HTTPS, move an old page to a new one, make pages load faster or keep private files private, and a wrong line can take the whole site down. This guide explains what the common rules do so that you can use them with confidence. The rules come from the .htaccess generator on this site, which builds the file in your browser, so nothing you type is uploaded.
What the file is and where it works
Apache reads the .htaccess file in a folder on every request for that folder and the ones below it, which makes it handy on shared hosting where you cannot edit the main server settings. Servers that copy Apache, such as LiteSpeed, read it too. It does not work on nginx, and static hosts such as Cloudflare Pages use their own _redirects and _headers files instead. The file name starts with a dot and has no extension, and some file managers hide it until you show hidden files.
Forcing HTTPS
The usual rule has three lines: RewriteCond %{HTTPS} off, RewriteCond %{HTTP:X-Forwarded-Proto} !https and RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]. The first line applies the rule only when the connection is not secure. The second line looks at a header that a proxy or CDN adds when it handled HTTPS itself. Without it, a site behind such a service can see every request as plain HTTP and redirect forever. The last line sends the visitor to the same address on https with a permanent redirect.
You need a working SSL certificate first, or visitors will see a security warning. Once HTTPS works on every part of the site, you can add HSTS, which tells browsers to use only HTTPS for a year. Do this last, because browsers remember it and a mistake is hard to undo.
Choosing www or no www
A site that answers on both example.com and www.example.com can be indexed twice. Pick one and redirect the other. The generator writes the www rule before the HTTPS rule and sends the visitor to the final https address directly. With the no-www choice and HTTPS on, a visit to http://www.example.com/page goes to https://example.com/page in one step, instead of two. Make the choice once and keep your links, sitemap and canonical tags on the same version.
301 and 302 redirects
Use 301 when an address has moved for good, so that search engines pass the old page's standing to the new one. Use 302 for a temporary move. The line is Redirect 301 /old-page /new-page, with the old path first. The old path matches by prefix, so /old-page/anything also goes to /new-page/anything. To send visitors to another site, give a full address as the target.
Avoid chains where A goes to B and B goes to C, because each hop is slower and some of the value can be lost. Point the old address straight at the last one. Keep redirects in place for a long time, commonly at least a year. Check one with curl -I followed by the address, which prints the status code and the Location header.
Compression and caching
Compression with mod_deflate shrinks text files such as HTML, CSS, JavaScript, JSON and SVG. It is not applied to images, which are already compressed. Caching with mod_expires tells browsers how long to keep a file before asking again. The generator lets you choose one week, one month, six months or one year for images, styles, scripts and fonts, and sets HTML to zero seconds so that changes show up straight away.
A long cache is a trade-off: visitors may keep an old stylesheet after you change it. The fix is to put a version or a hash in the file name, such as style.3f9a.css, so a changed file has a new address. With names like that, a one-year cache is safe.
Security headers and file protection
- X-Content-Type-Options: nosniff stops browsers from guessing a file's type. X-Frame-Options: SAMEORIGIN stops other sites from showing yours inside a frame, which blocks clickjacking.
- Referrer-Policy: strict-origin-when-cross-origin limits what is passed to other sites when visitors click away. Permissions-Policy: camera=(), microphone=(), geolocation=() switches those features off for the page.
- Options -Indexes stops Apache from listing the files in a folder that has no index page.
- The file-block rule denies hidden files such as .env and .htpasswd and backup, SQL and log files. It is a safety net, not a plan. Secrets and backups should be kept out of the public folder altogether.
Error pages, IP blocks and hotlinks
ErrorDocument 404 /404.html shows your own page for missing addresses. Give a path, not a full address. A full address makes Apache send a redirect instead of a real 404 status, which confuses search engines.
Blocking uses Require not ip inside a RequireAll block, and accepts single addresses and ranges such as 198.51.100.0/24. Hotlink protection refuses image requests that come from other sites, using the Referer header. That header can be blank or faked, so treat it as a deterrent for casual copying and nothing more.
If you get a 500 error
- A 500 error after an upload usually means a directive is not allowed on your host, a module such as mod_headers is missing, or there is a typo. The server's error log names the line.
- Remove the last block you added, or rename the file, and the site should return. Then add blocks back one at a time and test after each.
- The generator wraps header, caching and compression blocks in IfModule checks, which skip them quietly if a module is missing. Options and ServerSignature are not wrapped and can cause an error on hosts that restrict them.
- Keep a copy of the working file before every change.
Using the generator
- Tick the options you want, choose www or no www and the cache time, and add redirects, error pages, blocked addresses or a hotlink domain.
- Paths, addresses and the domain are checked before the file is built. HSTS is allowed only together with the HTTPS redirect.
- Copy or download the file. Some browsers drop the leading dot on download, so rename it to .htaccess if needed.
- On WordPress, put these rules above the block between # BEGIN WordPress and # END WordPress and do not edit inside it.
Put it into practice
.htaccess Generator
Build an Apache .htaccess file from tick boxes: HTTPS and www redirects, 301 redirects, compression, caching, security headers, error pag…
Runs in your browserHTTP Status Codes Reference
Look up any HTTP status code by number or keyword and see what it means, when servers send it and how to fix it, from 200 OK to 503 Servi…
Runs in your browserRobots.txt Generator
Build a valid robots.txt file with user-agent groups, allow and disallow rules, crawl delay and sitemap lines, with presets for WordPress…
Runs in your browserMeta Tag Generator
Generate the title, description, canonical, robots, Open Graph and X (Twitter) card tags for a page, with length checks and a live search…
Runs in your browserHow this guide is maintained
This page is reviewed when the related tools change. It describes what the published tools actually do: the tools run in your browser tab, so your files stay on your device. If you find something inaccurate, email [email protected] and it will be corrected.
Other guides
- How to merge PDFs without uploading them6 min · 17 November 2026
- OCR a scanned PDF: accuracy, limits and privacy7 min · 17 November 2026
- Compress a PDF without destroying readability6 min · 17 November 2026