.htaccess

Last updated 17 June 2026 2 min

.htaccess (short for "hypertext access") is a configuration file used by Apache web servers. Placed inside a directory, it lets you override the server's main configuration for that directory and everything beneath it — without needing root access to the server itself.

What it controls

A single .htaccess file can handle a surprising amount of work, including:

  • Redirects — 301s, 302s, and rule-based rewrites via mod_rewrite.
  • URL rewriting — turning ugly query strings into clean, readable URLs.
  • Access control — password-protecting directories, blocking IPs, restricting user agents.
  • Custom error pages — pointing 404s, 403s, and 500s to branded templates.
  • Caching and compression — setting Expires headers and enabling Gzip/Brotli.
  • HTTPS enforcement — forcing all traffic to the secure version of the site.
  • Canonical host rules — forcing www or non-www, removing trailing slashes.
  • MIME types and character encoding — telling browsers how to interpret files.

Why it's useful for SEO

Many technical SEO fixes on Apache hosting flow through .htaccess. Redirect chains, mixed canonical signals, www/non-www duplication, and missing HTTPS enforcement are all resolved here. A clean .htaccess keeps crawl signals consistent and prevents the duplicate-content and redirect-loop issues that erode rankings or make content inaccessible.

Caveats

  • Apache only. Nginx and IIS use different configuration models (nginx.conf, web.config).
  • Performance cost. Apache reads .htaccess on every request to a covered directory. Overly heavy rule sets can slow a site down.
  • Order matters. Rules execute top-down, and a misplaced rewrite rule can break an entire site.

Common pitfalls

  • Forgetting RewriteEngine On before any rewrite rules.
  • Trailing slash inconsistencies causing redirect loops.
  • Caching directives placed inside <IfModule> blocks for modules that aren't enabled.

When to use .htaccess

.htaccess can be used whenever you need server-level behaviour changes that don't require touching the application code — typically redirects and URL canonicalisation.

Disclaimer: All information contained herein is for informational purposes only. It is not advice or instructional.