Migrations
htaccess 301 Redirect Cheat Sheet: Every Pattern, One Hop, Plus Nginx
An htaccess redirect is a rule in your site's .htaccess file that tells the server "this page lives somewhere else now," and most of the snippets floating around the internet are fine until you paste three of them in a row. Then a visitor bounces from http to https, to www, to the new page, and Google gets a relay race instead of an address.
Below is every pattern I reach for on a migration, written as copy-paste blocks. Each one does its job in one hop (a single redirect, straight to the final URL), and each has its Nginx and Cloudflare equivalent so you're covered if your site moves off Apache later.
If you're staring at a list of 400 old URLs and a launch date, you can send me the details for a migration quote. Otherwise, grab a coffee and a backup of your current .htaccess.
How an htaccess 301 redirect actually works
A 301 redirect (a permanent forwarding address for a web page) tells browsers and Google that the old URL is retired for good. Google's documentation says a permanent redirect is a strong signal that the target should be the canonical URL, meaning the one that shows in search. A 302 is temporary, and Google doesn't treat it as that signal. If you're unsure which one you need, here's when a 302 is the right call.
On Apache (and LiteSpeed, which reads the same file), you have two tools:
- mod_alias gives you
RedirectandRedirectMatch. Simple and fast. - mod_rewrite gives you
RewriteRuleandRewriteCond. More power, more ways to shoot your foot.
Apache's own docs say simple redirects should use Redirect or RedirectMatch rather than RewriteRule. I agree for a handful of rules. On WordPress sites with real migrations, I usually write everything in mod_rewrite anyway, for one reason covered next.
Rule placement on WordPress
WordPress writes its own block between # BEGIN WordPress and # END WordPress, and it rewrites that block when you save permalinks. Anything you put inside gets wiped. Your redirects go above that block.
There's a subtler trap. Inside .htaccess, Apache runs mod_alias before mod_rewrite (the same Apache page spells this out), no matter which comes first in the file. So a Redirect line at the bottom still fires before a RewriteRule at the top. Mix the two and the order you see isn't the order you get. Pick one module per file and life gets calmer.
# Layout of a WordPress .htaccess with custom redirects.
# Your rules go first. WordPress may overwrite anything between its own markers.
RewriteEngine On
# ---- custom redirects start ----
RewriteRule ^old-page/?$ https://www.example.com/new-page/ [R=301,L]
# ---- custom redirects end ----
# BEGIN WordPress
# (WordPress manages this block. Don't edit it.)
# END WordPresshtaccess redirect examples: the cheat sheet
Each block below has a one-line note on what it does and what it can break. Swap example.com for your domain. All targets use https, because a redirect to http just creates a second hop later.
1. One page to another page
# Sends /old-page and /old-page/ to the new URL. Could break: nothing, unless
# /old-page/ has child pages you still need (this rule only matches the page itself).
RewriteRule ^old-page/?$ https://www.example.com/new-page/ [R=301,L]The /? makes the trailing slash optional, so both versions are caught. In .htaccess the pattern has no leading slash. That one detail causes half the "my redirect does nothing" emails.
The mod_alias version, if you prefer it:
# Same result with mod_alias. The ^ and $ anchors stop it matching longer paths.
RedirectMatch 301 ^/old-page/?$ https://www.example.com/new-page/2. Old file names (.html, .php) to clean URLs
# /about-us.html -> /about/. The dot is escaped so it means a literal dot.
# Could break: nothing, as long as the new page exists.
RewriteRule ^about-us\.html$ https://www.example.com/about/ [R=301,L]An unescaped dot means "any character," so about-us.html would also match about-usXhtml. Harmless most days, sloppy on a 500-line map.
3. A whole folder, keeping everything after it
# /blog/anything -> /news/anything. Keeps sub-paths. Could break: any URL under
# /blog/ that should go somewhere else. Put those specific rules ABOVE this one.
RewriteRule ^blog/(.*)$ https://www.example.com/news/$1 [R=301,L]The (.*) captures the rest of the path and $1 pastes it back. Order matters here: specific rules first, folder-wide rules after, because [L] stops at the first match.
4. A whole domain to a new domain
# Every URL on the old domain -> same path on the new domain, one hop.
# Lives in the OLD site's .htaccess. Could break: subdomains you forgot to list.
RewriteCond %{HTTP_HOST} ^(www\.)?old-domain\.com$ [NC]
RewriteRule ^(.*)$ https://www.new-domain.com/$1 [R=301,L]This is the backbone of a domain move. Google asks you to keep these redirects generally at least one year, and I'd keep them for as long as you own the old domain.
5. Old URLs with query strings
RewriteRule never sees the query string (the part after ?). You match it with a RewriteCond:
# /product.php?id=42 -> /products/blue-widget/. QSD drops ?id=42 from the target.
# Could break: other ?id= values. This matches id=42 exactly, nothing else.
RewriteCond %{QUERY_STRING} ^id=42$
RewriteRule ^product\.php$ https://www.example.com/products/blue-widget/ [R=301,L,QSD]By default, mod_rewrite copies the old query string onto the target. That's how you end up with /products/blue-widget/?id=42 living forever in Google. QSD (query string discard) stops it. Leave QSD off when you want tracking tags like utm_source to survive the redirect.
6. Case-insensitive match
# /Services/, /SERVICES/ and /services/ all go to the new page.
# Could break: nothing in practice. Old Windows-hosted sites need this a lot.
RewriteRule ^services/?$ https://www.example.com/what-we-do/ [R=301,L,NC]7. Pages that are gone for good: 410
Not every old URL deserves a redirect. Sending a deleted page to your homepage looks like a soft 404 (an error page pretending to be a real page) to Google. If there's no close replacement, say so:
# Returns 410 Gone for a retired page and everything under /old-promo/.
# Could break: any live page that happens to match. Check the pattern against your URL list.
RewriteRule ^old-promo(/.*)?$ - [G,L]The [G] flag returns a 410 Gone, which tells Google the page existed and was removed on purpose. The mod_alias version is Redirect gone /old-promo, which also covers everything under that path.
8. HTTPS and www together
This one gets its own guide, because doing it in two separate rules is exactly how chains happen. Here's the short version, and the long version covers HTTPS plus www in one hop, including the Cloudflare loop fix.
# http://example.com/x, http://www.example.com/x and https://example.com/x
# all go straight to https://www.example.com/x. Could break: sites behind
# a proxy or Cloudflare Flexible SSL (see the linked guide).
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L,NE]The one-hop rule (and how to check it)
Googlebot will follow up to 10 redirects in a row, but Google's site-move guide advises keeping chains to ideally no more than 3 and redirecting straight to the final URL. My standard is stricter: one hop, every time.
Chains usually come from layers stacking up over the years:
- A 2016 rule sends
/services.htmlto/services/. - A 2021 redesign sends
/services/to/what-we-do/. - The HTTPS rule runs first anyway, adding a hop at the front.
The fix is boring and effective. When you add a new redirect, search the file for any rule pointing at the old target and update it to point at the new final URL.
Test from the command line rather than the browser. Browsers cache 301s hard, so yesterday's mistake can haunt your laptop for days.
# Shows every hop: status code and Location header. One "301" line then a "200" is the goal.
curl -sIL http://example.com/old-page | grep -iE "^(HTTP|location)"For a big list, loop over a text file of old URLs and print each final URL plus the number of hops. Any line with more than one hop goes on the fix list.
# urls.txt = one old URL per line. Prints hops, final status and final URL.
while read -r u; do
curl -s -o /dev/null -L -w "%{num_redirects} hops %{http_code} %{url_effective}\n" "$u"
done < urls.txtThe same redirects on Nginx and Cloudflare
Nginx doesn't read .htaccess at all, so these rules go in the server block. Cloudflare can redirect before the request reaches your server. Here's the cheat sheet side by side.
| Pattern | Apache (.htaccess) | Nginx (server block) |
|---|---|---|
| One page | RewriteRule ^old/?$ https://ex.com/new/ [R=301,L] |
location = /old/ { return 301 https://ex.com/new/; } |
| Folder with sub-paths | RewriteRule ^blog/(.*)$ https://ex.com/news/$1 [R=301,L] |
location ~ ^/blog/(.*)$ { return 301 https://ex.com/news/$1; } |
| Gone for good | RewriteRule ^old-promo/?$ - [G,L] |
location = /old-promo/ { return 410; } |
| Whole domain | RewriteCond %{HTTP_HOST} + RewriteRule ^(.*)$ |
return 301 https://new.com$request_uri; |
The Nginx return directive stops processing right away, and $request_uri keeps the original path and query string. One Nginx quirk: location blocks match the path only, never the query string. For exact-match redirects, add $is_args$args to the target if you want query strings carried over.
On Cloudflare, Bulk Redirects handle long lists of exact URL pairs. They're uploaded as a CSV and can't use regular expressions. Each row looks like this, with no header row allowed:
# source, target, status, preserve query string, include subdomains, subpath matching, preserve path suffix
example.com/old-page,https://www.example.com/new-page/,301,,,,
example.com/blog/,https://www.example.com/news/,301,TRUE,,TRUE,TRUETwo limits worth knowing: Bulk Redirects only return 301, 302, 307 or 308, so your 410s stay on the server, and Cloudflare's redirects fire before your server ever sees the request. If both layers redirect the same URL, the Cloudflare one wins and the .htaccess rule never runs.
Writing hundreds of these by hand is how typos happen. I built a free redirect generator that turns a spreadsheet into Apache, Nginx or Cloudflare rules, including the 410s and the query-string cases.
When .htaccess redirects go wrong
Three failure modes cover most of the panicked messages I get.
The whole site shows a 500 error. A typo in .htaccess takes down every page, not just the redirect. Keep a copy of the working file, edit over SFTP or your host's file manager, and reload right away. If it already happened, here's how to fix a 500 error after an .htaccess edit.
"Too many redirects." Two rules are bouncing a URL back and forth. Usually it's an HTTPS rule fighting a proxy or Cloudflare setting. Comment out your rules one at a time until it stops.
The redirect works, but traffic still dropped. That's rarely the syntax. It's the map: old URLs nobody listed, pages sent to the homepage, or a staging noindex that went live. My migration checklist covers the map-building part, which is where most of the real work lives.
Vlad's take
I've written a lot of .htaccess and regex redirects over 20+ years in search, mostly during migrations and hack cleanups. The syntax is the easy part. The skill is in the list: every URL that ever earned a click or a link, mapped to its closest real match, tested before DNS changes. A perfect rule pointing at the wrong page is still the wrong page.
Final Thoughts
Keep your redirects above the WordPress block, stick to one module per file, escape your dots, and test every old URL until it shows one hop and a 200. Use 410 for pages that are truly gone, and keep the rules in place for at least a year.
If you'd rather hand the whole thing to someone, a migration with a tested redirect map is exactly what I do. You can tell me about your move here, and I reply with a plan and a fixed quote within one business day.
htaccess Redirect FAQs
Where do I put redirects in a WordPress .htaccess file?
Above the # BEGIN WordPress line. WordPress rewrites everything between its own markers when you save permalinks or when some plugins update, so rules placed inside that block can vanish without warning. Put RewriteEngine On once at the top, then your redirects, then the WordPress block. Keep a dated backup of the working file before every edit.
Should I use Redirect or RewriteRule in .htaccess?
For a few simple page-to-page redirects, Redirect or RedirectMatch is cleaner and Apache recommends it. For WordPress sites with many rules, query strings, or domain conditions, I use RewriteRule throughout. The main thing is not to mix them, because in .htaccess mod_alias rules run before mod_rewrite rules no matter where they sit in the file.
Why is my .htaccess 301 redirect not working?
The usual suspects: a leading slash in the RewriteRule pattern (it shouldn't have one in .htaccess), the rule sitting below WordPress's catch-all block, a browser serving a cached old redirect, or the server ignoring .htaccess entirely because it runs Nginx. Test with curl -sIL instead of your browser, and check with your host which web server you're on.
How long should I keep 301 redirects after a migration?
Google recommends keeping them as long as possible, generally at least one year, so it can transfer signals to the new URLs. In practice, keep them as long as you own the old domain or the old URLs have backlinks. Redirect rules cost almost nothing to keep, and removing them early turns earned links into 404s.
Can Cloudflare return a 410 instead of a redirect?
Not with Bulk Redirects. They only send 301, 302, 307 or 308 responses. Keep your 410 rules on the origin server in .htaccess or the Nginx config, and make sure no Cloudflare redirect matches those same URLs first, since Cloudflare's redirects run before the request reaches your server.
Want a second pair of eyes on your site?
Tell me what's going on in the website migration questionnaire. I reply with a plan and a fixed quote within one business day.