Guide · Apache 2.4

The .htaccess redirect cookbook

Tested, copy-paste recipes for 301 redirects on Apache — the web server behind most shared hosting, cPanel, Cloudways and WordPress sites. The original 301site snippets from 2014, brought up to date.

Before you start

.htaccess is a plain text file in your website’s root folder (the one containing your homepage). Apache reads it on every request, top to bottom. A few things save a lot of pain:

  • Back it up first. One typo gives you a “500 Internal Server Error” on every page. If that happens, restore the backup — nothing is permanently broken.
  • Use mod_rewrite throughout. Apache has two redirect systems: Redirect/RedirectMatch (mod_alias) and RewriteRule (mod_rewrite). They run at different times, so mixing them causes confusing results. Every recipe here uses RewriteRule for that reason.
  • You only need RewriteEngine On once, above your first rule.
  • Test with R=302, then switch to R=301. Browsers cache 301s for a long time, which makes a mistake hard to undo.

In .htaccess, the path a RewriteRule sees has no leading slash: for https://example.com/about/team it matches against about/team. That’s why patterns start ^about, not ^/about.

Redirect one page

The most common redirect there is. The pattern is a regular expression, so dots need a backslash, and ^…$ makes it match that exact path only.

.htaccess
RewriteEngine On
RewriteRule ^old-page\.html$ /new-page [R=301,L]
RewriteRule ^about-us$ https://example.com/about [R=301,L]

R=301 makes it a permanent redirect; L means “last — stop processing further rules for this request”. Any query string (like ?utm_source=…) is passed along automatically. To drop it, end the target with a ?: /new-page?.

You’ll see older guides use Redirect 301 /old-page.html /new-page. It works, but it matches the start of the path — so it also redirects /old-page.html/anything — and it doesn’t mix well with WordPress’s own rewrite rules.

Pages with query strings

Old dynamic URLs like /product.php?id=42 can’t be matched by RewriteRule alone — the query string isn’t part of the path. Add a RewriteCond above it:

.htaccess
RewriteCond %{QUERY_STRING} ^id=42$
RewriteRule ^product\.php$ /shop/blue-widget? [R=301,L]

The ? at the end stops ?id=42 following visitors to the new URL. Each RewriteCond only applies to the single RewriteRule directly below it. To keep a value from the query string, capture it and reuse it with %1:

.htaccess
# /product.php?id=42  →  /products/42
RewriteCond %{QUERY_STRING} (?:^|&)id=(\d+)(?:&|$)
RewriteRule ^product\.php$ /products/%1? [R=301,L]

Move a whole domain

Put this in the .htaccess of the old domain. Every page lands on the same path on the new domain — old.com/about?x=1 goes to new.com/about?x=1 — which is exactly what search engines want to see.

.htaccess (old domain)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?old-site\.co\.uk$ [NC]
RewriteRule ^(.*)$ https://new-site.com/$1 [R=301,L]

Keep the old domain alive. The redirect only works while the old domain’s DNS, hosting and SSL certificate keep answering. Let the domain lapse and every old link breaks — and someone else can buy it. Renew it for years.

Then, in Google Search Console, verify both domains and use the Change of Address tool on the old one. More in Redirects & SEO.

HTTPS and www in one hop

The most-copied snippet on the internet is usually two separate rules — one for HTTPS, one for www — which gives visitors to http://example.com a two-hop chain. This does both in a single redirect:

.htaccess
RewriteEngine On
# Everything → https://example.com (no www), in one hop
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]

Prefer www? Flip the host check:

.htaccess
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]

Behind Cloudflare, a load balancer or Varnish? Your server may only ever see plain HTTP from the proxy, so %{HTTPS} is always off and you get an infinite loop. Swap the first condition for RewriteCond %{HTTP:X-Forwarded-Proto} !https [OR] — or simply use your host’s “Force HTTPS” switch (Cloudways, cPanel and Cloudflare all have one) and keep only the www rule here.

Move a folder

Moves /blog, /blog/ and everything under it, keeping the rest of the path: /blog/2024/hello → /news/2024/hello.

.htaccess
RewriteRule ^blog(?:/(.*))?$ /news/$1 [R=301,L]

# …or to another domain entirely:
RewriteRule ^shop(?:/(.*))?$ https://shop.example.com/$1 [R=301,L]

The original 2014 recipe here also forced a new file extension for everything in the folder. That still works, as long as the new files exist:

.htaccess
# /directory/page.html or .php  →  new site, always .php
RewriteRule ^directory/(.*)\.(?:php|html)$ https://new-site.com/directory/$1.php [R=301,L]

Everything to one page

Sometimes a site really is closing and there’s only one page left to send people to:

.htaccess
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/goodbye/?$
RewriteRule ^ https://new-site.com/goodbye? [R=301,L]

Use sparingly. Google treats a redirect from a specific page to an unrelated page (like the homepage) as a “soft 404”: the old URL is dropped and its value isn’t passed on. If the content exists anywhere on the new site, redirect page to page instead — the bulk generator makes that quick.

Change file extensions

.html → clean URLs (/about.html → /about), while still serving the original file behind the scenes. The trick is matching %{THE_REQUEST} — the raw line the browser sent — so Apache’s own internal rewrite can’t trigger the redirect again and loop:

.htaccess
RewriteEngine On
# 1. /folder/index.html → /folder/
RewriteCond %{THE_REQUEST} \s/+((?:[^?\s]*/)?)index\.html[?\s] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# 2. /page.html → /page
RewriteCond %{THE_REQUEST} \s/+([^?\s]*?)\.html[?\s] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# 3. Quietly serve /page from page.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

.html → .php, only where the PHP version exists:

.htaccess
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+)\.html$ /$1.php [R=301,L]

Trailing slashes

Pick one style and stick to it; /about and /about/ are different URLs to a search engine. Remove them:

.htaccess
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]

…or add them (skipping real files and anything with an extension, like logo.png):

.htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(/$|\.[a-zA-Z0-9]{1,5}$)
RewriteRule ^ %{REQUEST_URI}/ [R=301,L]

Pages that are gone

If there’s genuinely no replacement, don’t redirect — tell the truth with 410 Gone. The - means “don’t change the URL”, and G sends the 410:

.htaccess
RewriteRule ^discontinued-product$ - [G]
RewriteRule ^old-campaign/ - [G]
ErrorDocument 410 /gone.html

WordPress

WordPress writes its own block into .htaccess and may rewrite anything between its markers. Put your redirects above it:

.htaccess
# --- My redirects (keep above WordPress) ---
RewriteEngine On
RewriteRule ^old-page/?$ /new-page/ [R=301,L]

# BEGIN WordPress
# …WordPress’s own rules — don’t edit between these markers…
# END WordPress

WordPress already redirects some old post URLs by itself, and a redirect plugin gives non-technical editors a friendly screen — but for anything site-wide (domains, HTTPS, folders), server rules are faster and also catch images and PDFs. More in PHP & platforms.

Debugging

SymptomLikely cause
500 error on every pageA typo, or mod_rewrite isn’t enabled. Restore your backup and add rules one at a time.
Nothing happens at allThe server isn’t Apache (Nginx ignores .htaccess), AllowOverride is off, the file is in the wrong folder, or an earlier rule with [L] matched first.
“Too many redirects”A loop: often HTTPS forced here and by a CDN/host setting, or a rule whose target also matches its own pattern.
It worked, then I changed it and it didn’tYour browser cached the old 301. Test in a private window or with curl -I.
Two or three hops instead of oneSeparate rules for HTTPS, www and slashes. Combine them, and trace the URL to confirm.
Redirects go to http:// then bounce to https://Apache is behind a proxy (Cloudflare, Varnish, Nginx) and builds relative targets like /new-page with the scheme it sees — plain HTTP. Use full https://example.com/new-page targets.
%20 or odd characters doubled upApache is escaping an already-encoded URL. Add the NE (no escape) flag to that rule.

From a terminal, curl -sIL http://example.com/old-page | grep -iE "^(HTTP|location)" prints every hop’s status and destination.