.htaccess Redirect Rules Explained (With Copy-Paste Examples)

301 vs 302, Redirect vs RewriteRule, HTTPS and www canonicalisation, and the redirect mistakes that quietly cost organic traffic.

Redirects are the plumbing of a website migration. Get them right and rankings transfer within weeks; get them wrong and you lose traffic in ways that are hard to diagnose months later.

This guide explains the rules you will actually use in Apache .htaccess, when to use each one, and how to avoid the classic mistakes. If you want the rules generated for you (including the Nginx equivalents), use the .htaccess redirect generator.

Key takeaways

  • Use 301 for anything permanent. Use 302 only for genuinely temporary situations.
  • Prefer Redirect for simple one-to-one moves and RewriteRule when you need patterns or conditions.
  • Canonicalise HTTPS and www once, at the top of the file, before anything else.
  • Never chain redirects. Point old URLs at the *final* destination.

Status codes that matter

Code — Meaning — Use it when

301 — Moved permanently — The URL has changed for good — the default choice

308 — Permanent, method preserved — Permanent redirect for POST endpoints

302 — Found (temporary) — A/B tests, short campaigns, maintenance

307 — Temporary, method preserved — Temporary redirect for non-GET requests

410 — Gone — Content deleted with no replacement

A 301 passes ranking signals and is cached aggressively by browsers — which is exactly why you should not use it for anything you might reverse. If a page is genuinely gone and has no equivalent, a 410 is more honest than redirecting everyone to the homepage.

Simple redirects with mod_alias

For fixed, one-to-one moves, Redirect is the clearest tool:

Redirect 301 /old-page /new-page
Redirect 301 /pricing.html /pricing
Redirect 410 /discontinued-product

Paths are matched from the document root and the remainder of the path is appended, which surprises people: Redirect 301 /blog /news sends /blog/post-1 to /news/post-1. That is usually what you want for a folder move, and occasionally exactly what you do not want.

Use RedirectMatch when you need a pattern but no conditions:

RedirectMatch 301 ^/products/([0-9]+)/?$ /shop/item-$1

Pattern redirects with mod_rewrite

RewriteRule gives you conditions, capture groups and query-string control:

RewriteEngine On

# Force HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

# Force non-www (pick one canonical host and stick to it)
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

# Strip trailing slashes on files, add them on directories
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=301,L]

Flag reference for the ones you will use:

  • R=301 — issue an external redirect with this status
  • L — last rule; stop processing on a match
  • NC — case-insensitive match
  • QSA — append the original query string
  • QSD — discard the original query string

Query strings

RewriteRule patterns do not see the query string. Match it with a condition:

RewriteCond %{QUERY_STRING} ^id=42$
RewriteRule ^article\.php$ /blog/my-post? [R=301,L]

The trailing ? drops the original query string. Without it, the old parameters follow the visitor to the new URL.

Order matters

Rules are evaluated top to bottom. A sensible file structure:

  1. RewriteEngine On
  2. HTTPS canonicalisation
  3. Host (www) canonicalisation
  4. Specific one-to-one legacy redirects
  5. Pattern-based redirects
  6. Application front-controller rules (Laravel, WordPress) last

Putting your front controller first means it swallows every request and your redirects never fire.

Five mistakes that cost traffic

  1. Redirect chains. /a → /b → /c wastes crawl budget and dilutes signals. Update /a to point straight at /c.
  2. Redirect loops. Usually caused by two canonicalisation rules disagreeing, or a rule missing its [L] flag. Test with curl -I before deploying.
  3. Redirecting everything to the homepage. Search engines treat mass homepage redirects as soft 404s. Map to the closest equivalent or return 410.
  4. Mixing 302 into a migration. Temporary redirects do not consolidate signals the way permanent ones do.
  5. Forgetting the canonical tag. After redirecting, make sure the destination page's canonical URL points at itself, not the old URL.

Verifying your rules

curl -sIL https://example.com/old-page | grep -E 'HTTP/|location'

You want to see exactly one 301 hop landing on a 200. Anything with two hops is a chain worth flattening. After a migration, re-submit your sitemap and watch the crawl stats — a healthy migration shows the old URLs dropping out and new ones being indexed within a few weeks.

Generate the rules

The .htaccess redirect generator turns a list of old and new paths into ready-to-paste Apache and Nginx configuration, with the correct status codes and flags applied. Pair it with the meta tag generator to confirm the destination pages carry a self-referencing canonical and complete social tags.