Developer Tools

.htaccess Guide: Redirects, HTTPS and Security Rules

A practical .htaccess guide covering forced HTTPS, www canonicalization, 301 redirects, security headers and browser caching, with tested example snippets.

3 min read·Updated 2026-08-01

The .htaccess file lets an Apache server change behaviour per directory: force HTTPS, pick one canonical hostname, move old URLs and harden responses without touching the main server config. The convenience comes with a risk — one broken rule can return a 500 error for every page the moment you save. This guide covers the five jobs most sites need, in the order you should add them.

Force HTTPS before anything else

Redirect plain HTTP first, or later rules can capture plaintext requests and create loops:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

The R=301 flag marks the move as permanent for browsers and crawlers; the L flag stops rule processing so nothing else fires on the redirected request.

Four habits keep .htaccess edits safe:

  • Back up the current file before every change.
  • Add one rule at a time, then reload the site in a private window.
  • Test each redirect with curl -I before trusting a browser, since browsers cache permanent redirects.
  • Keep a plain text copy of the last working version — recovery is then a rename away.

Canonicalize www or non-www

Serving both hostnames means duplicate content twice over. Choose one and 301 the other into it:

RewriteCond %{HTTP_HOST} ^www[.]example[.]com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

Swap the two lines if you prefer the www version. Always pair this rule with the HTTPS redirect above, and test both orders.

301 redirects for moved pages

For simple path moves you do not need mod_rewrite at all — a single line works:

Redirect 301 /old-pricing.html /pricing.html

Pick the right status code when the change is not permanent:

CodeMeaningUse when
301Moved permanentlyOld URL should never return
302Found (temporary)Short-lived campaign page
307Temporary, method preservedPOST must stay POST during maintenance
308Permanent, method preservedPermanent move that must keep POST

Generate bulk rewrite maps with the Redirect Code Generator (301/302/307/308) rather than typing flags from memory.

Security headers worth sending

Headers are cheap hardening. Wrap them in a module check so a missing module does not break the site:

<IfModule mod_headers.c>
Header always set X-Content-Type-Options "nosniff"
Header always set X-Frame-Options "SAMEORIGIN"
Header always set Referrer-Policy "strict-origin-when-cross-origin"
</IfModule>

Adding Options -Indexes also stops directory listing when no index file is present.

Browser caching rules

Expiry headers tell browsers how long to reuse files, cutting repeat-visit requests dramatically:

<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/css "access plus 1 year"
ExpiresByType image/jpeg "access plus 6 months"
ExpiresByType text/html "access plus 0 seconds"
</IfModule>

HTML must stay at zero seconds so deploys show up immediately; fingerprints like CSS and images can safely live for a year.

Frequently asked questions

Why does my site show a 500 error after editing? Usually a missing module, a stray quote or a rule that is not allowed in a per-directory context. Rename the file to .htaccess.off, confirm the site loads, then reintroduce rules one at a time.

Does .htaccess work on Nginx or shared hosting? Only on Apache. Nginx uses server and location blocks in its own config; most shared hosts do run Apache, so .htaccess applies automatically.

Do I still need caching headers if I use a CDN? Yes — the CDN benefits from them too, and direct hits to your origin still need them. Use the .htaccess Generator to assemble a complete, balanced file.

Tools used in this guide

Related guides