Skip to content
Server & .htaccess

WordPress .htaccess Redirect Rules: 301s Done Right

Move a URL without losing rankings: which WordPress .htaccess redirect directive to use, where the rules must sit, and how to test without lockout.

Published Updated

Moving a URL and want the old one to keep its rankings? You need a 301 in .htaccess, placed where WordPress will not erase it, written with the right directive, and pointed at the exact URL WordPress actually serves. Get any of those three wrong and you get a redirect chain, a loop, or a 500 that locks you out of wp-admin.

The decision, the placement rule, the common recipes, and the test, in that order.

TL;DR: Use Redirect for one known path, RedirectMatch when one regex covers a whole section, and RewriteRule only when the redirect depends on a condition (hostname, protocol, query string), because mod_alias can’t test those. Put custom rules above # BEGIN WordPress, never inside it. Test with curl -sI, not a browser, and start with a 302 until the destination is confirmed.

Which directive: Redirect, RedirectMatch, or RewriteRule

There are two Apache modules in play and they are not interchangeable.

DirectiveModuleBest forSyntaxTests a condition?
Redirectmod_aliasOne known old path → one new pathRedirect 301 /old-page/ https://example.com/new-page/No
RedirectMatchmod_aliasA whole section moving under one patternRedirectMatch 301 ^/blog/(.*)$ https://example.com/articles/$1No
RewriteRulemod_rewriteHostname, protocol, query string, user agentRewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
Yes

Decision flow, in short: does the redirect depend on anything other than the URL path itself, such as the host, the protocol, a query parameter or a header? If yes, that’s a condition, and only RewriteRule can test it. If no, stay in mod_alias: one path is Redirect, a whole pattern of paths is RedirectMatch.

Need to redirect a URL?
↓
Does it depend on a condition: host, protocol, query string, header?
↓ no↓ yes
One path, or a whole section pattern? → Redirect or RedirectMatch
→ RewriteRule with RewriteCond

Redirect (mod_alias): one known path to one destination. The simplest thing that works:

Redirect 301 /old-page/ https://example.com/new-page/

Three behaviours to know. It matches on a path prefix, not an exact string, so /old-page/ also catches /old-page/anything/ and appends /anything/ to the destination. Apache’s docs add that “only complete path segments are matched”, so Redirect 301 /service catches /service/x but not /servicefoo.txt. It carries the query string across automatically. And the same docs warn: “Note that POSTs will be discarded.” Point forms, webhooks and API clients at the new URL instead of relying on the redirect.

RedirectMatch (mod_alias): one regex covering many URLs. This is how you move a whole section:

RedirectMatch 301 ^/blog/(.*)$ https://example.com/articles/$1

The capture group (.*) becomes $1 in the destination, so /blog/hello-world/ lands on /articles/hello-world/. Note the leading slash: mod_alias patterns match the full URL path.

Terminal output from curl showing 301 responses from Apache: old-page redirects to new-page, keeping the extra path and the query string, and blog/hello-world redirects to articles/hello-world
Responses from Apache 2.4.58 with these two rules in .htaccess, typeset for readability rather than a raw terminal capture. The Redirect line kept the extra path and the query string, and RedirectMatch filled in the captured part of the URL. You can reproduce this with curl -sI against any server running these two lines.

RewriteRule (mod_rewrite): required when the redirect depends on a condition: the hostname, the protocol, a query string, a user agent. Nothing in mod_alias can test those.

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

The pattern in a per-directory .htaccess has the leading slash stripped. That is why it is ^(.*)$ here and ^/blog/ in the mod_alias example. Apache’s mod_rewrite docs put it bluntly: a pattern starting with ^/ never matches in per-directory context. Mixing that up is a common reason a pasted rule silently does nothing.

Default to mod_alias. Reach for RewriteRule only when you need a condition. Fewer regexes, fewer ways to be wrong.

Where the rules must go

Custom redirects belong above the # BEGIN WordPress line. Not inside it.

# --- custom redirects ---
Redirect 301 /old-page/ https://example.com/new-page/
RedirectMatch 301 ^/blog/(.*)$ https://example.com/articles/$1

# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Two independent reasons:

  1. WordPress rewrites its own block. Opening Settings → Permalinks calls flush_rewrite_rules(), which discards everything between the markers and regenerates it. You don’t even have to click Save. A rule you put inside disappears the next time anyone opens that screen, possibly months later, with nobody connecting the two events.
  2. The catch-all ends rewriting. The WordPress block ends with a rule sending every non-file, non-directory request to index.php. A RewriteRule placed after it never runs for those URLs. In our 2026-11-16 test (Apache 2.4.58), a RewriteRule redirect placed after the block was ignored and index.php answered with a 200.

If the whole WordPress block above your redirects has gone missing (posts 404 but the homepage still loads), see restoring WordPress’s default .htaccess file before you add any more rules on top of it. If posts are 404ing for a different reason (the WordPress block is still there and opening the Permalinks screen doesn’t fix it), that’s a separate problem covered in WordPress posts returning 404 but the homepage works.

One complication: mod_alias and mod_rewrite don’t simply run in file order. In the same test, a Redirect line placed after the WordPress block still worked. And when one URL had both a Redirect and a RewriteRule [R=301], the RewriteRule won whichever came first in the file. Apache’s own docs describe the order in .htaccess the other way round (“in per-directory context (.htaccess), mod_alias runs first”), so don’t rely on either. If you need both modules on overlapping paths, pick one module for that path and stay inside it.

Common recipes

Every recipe below goes above # BEGIN WordPress. We ran each one on 2026-11-16 against Apache 2.4.58 (one-line index.php in place of WordPress) and the comment shows the response we got. Replace example.com with your domain.

Old domain to new domain, keeping the path and query string (in the old domain’s .htaccess, or one shared by both):

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?old-example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
# old-example.com/2026/10/post-name/?x=1 -> 301 https://example.com/2026/10/post-name/?x=1

www to the bare domain:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
# www.example.com/some/path/?x=1 -> 301 https://example.com/some/path/?x=1

Write the target with https:// so a visitor on http://www. lands on the final URL in one hop, not two. The reverse (bare domain to www) is the RewriteRule example in the table above. Set the WordPress Address and Site Address in Settings → General to the same host. WordPress’s canonical redirect switches between www and non-www to match the Site Address (read from redirect_canonical() in the source), so if the two disagree, visitors bounce back and forth in a loop.

A query-string URL such as /?p=123: Redirect and RewriteRule patterns never see the query string, so test it with RewriteCond:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^p=123$
RewriteRule ^$ https://example.com/new-page/ [R=301,L,QSD]
# /?p=123 -> 301 https://example.com/new-page/

The QSD flag drops the old query string from the target. Without it, mod_rewrite would append ?p=123 to the new URL.

A permanent move that must keep POST requests: use 308 instead of 301.

Redirect 308 /old-308/ https://example.com/new-308/
# /old-308/ -> HTTP/1.1 308 Permanent Redirect

RFC 9110 says a client may change a POST to a GET after a 301 or 302, and points to 308 (permanent) and 307 (temporary) when that’s unwanted. Google treats 301 and 308 the same way, as a permanent move.

HTTP to HTTPS has its own traps behind proxies and CDNs, so it’s covered in WordPress mixed content after moving to HTTPS, with tested rules.

Trailing slashes

WordPress’s own canonical redirect adds a trailing slash to most permalinks. So this:

Redirect 301 /old-page/ https://example.com/new-page

produces two hops: your 301 to the slashless URL, then WordPress’s own 301 adding the slash. Every hop adds a round trip for the visitor, and Google’s docs say its crawlers follow at most 10 redirect hops by default.

Load the destination in a browser first, copy the URL exactly as it settles in the address bar, and use that. If your permalink structure ends in .html or has no trailing slash, match that instead. There is no universally correct answer here, only “match what WordPress serves.”

Testing without locking yourself out

.htaccess is read on every request. A bad line returns a 500 for the entire site including wp-admin, so the recovery path has to exist before you need it.

Back up the file first. Over SSH:

cp /path/to/wordpress/.htaccess /path/to/wordpress/.htaccess.bak

If the site 500s, copy the backup back over the broken file and the site returns immediately. Keep an SFTP session already open in another window; logging in from scratch while the site is down is where the panic starts. Note that apachectl configtest does not parse .htaccess, so it will report a healthy config while your site is dead.

Test with curl, not a browser. Browsers can cache a 301 and will happily show you yesterday’s result:

curl -sI https://example.com/old-page/ | head -n 5

Read two things: the status line should say 301, and the Location header should be the exact final URL. To see the whole chain, follow it:

curl -sIL --max-redirs 10 https://example.com/old-page/ | grep -iE '^(HTTP|location)'

The -i matters. Over HTTP/2, header names arrive in lower case (location:), and the status line reads HTTP/2 301. In our test against Apache with HTTP/2, a case-sensitive grep -E '^(HTTP|Location)' printed only the status line and hid the redirect target.

More than one 301 line means you built a chain. A loop never settles: curl gives up with “Maximum (10) redirects followed” at the limit you set. Without --max-redirs, curl’s default limit is 50, so a loop takes a while to fail. We saw both messages with curl 8.5.0.

Use 302 while you are still unsure. RFC 9110 calls a 301 “heuristically cacheable”, so a browser may keep it without being told to. A 302 has no such rule, so a mistake is reversible in seconds. Switch to 301 once curl shows the destination you intended. Google treats a 302 as temporary, so don’t leave it in place for a permanent move.

If a rule seems to do nothing at all, confirm Apache is even reading the file. AllowOverride None on the directory makes Apache ignore .htaccess entirely, and nginx ignores it completely, so ask your host which server you’re on before debugging regexes. The WordPress .htaccess generator assembles the WordPress block plus force-HTTPS and www-to-bare-domain rules in Apache 2.4 syntax. It writes the root-install version, so edit RewriteBase for a subfolder.

When not to use .htaccess at all

WordPress already handles one common case on its own: when you change a published post’s slug, it remembers the old one and sends a 301 to the new permalink (wp_old_slug_redirect(), since WordPress 2.1). You don’t need a rule for that.

For a handful of redirects that editors need to manage themselves, a redirect plugin storing rules in the database is the better tool. It survives host migrations and does not require SSH. The tradeoff is real: PHP has to boot to serve the redirect, which is slower than Apache answering directly.

Use .htaccess for structural, permanent moves: a domain change, a section rename, forcing HTTPS or www. Use a plugin for one-off editorial redirects. Doing both is fine as long as you know which layer owns which URL, because a redirect defined in two places is a bug waiting for a bad afternoon.

The same file that holds your redirects is also where the useful security rules live. See WordPress .htaccess security rules that actually help for the handful worth adding. And if the move you’re making is HTTP to HTTPS specifically, pair the redirect with a database check: forcing the protocol in .htaccess doesn’t fix hardcoded http:// URLs already stored in posts and widgets, which is what WordPress mixed content after moving to HTTPS walks through.

Sources

William Lopez

William Lopez is the founder of Odie Themes. He builds the free WordPress tools and themes on this site and has been on WordPress.org since December 2019.

WordPress.org About

FAQ

Questions

Where do custom redirects go in the WordPress .htaccess file?

Above the # BEGIN WordPress line, never between the markers. WordPress regenerates everything inside those markers whenever anyone opens the Permalinks screen, so a rule placed inside is deleted without warning. Placement also matters for order: the WordPress block ends in a catch-all that sends unmatched requests to index.php.

Should I use Redirect, RedirectMatch or RewriteRule for a 301?

Use Redirect for a single known path, RedirectMatch when one pattern covers many URLs, and RewriteRule when the redirect depends on a condition such as hostname, protocol or query string. Redirect and RedirectMatch come from mod_alias and are simpler. RewriteRule comes from mod_rewrite and is the only one that can test conditions.

Why does my .htaccess redirect cause a redirect loop?

Usually the destination still matches the rule that sent the visitor there, so the rule fires again forever. The other common cause is forcing HTTPS behind a load balancer or CDN, where Apache sees plain HTTP on every request even though the visitor is on HTTPS. There, test the X-Forwarded-Proto header with =http, and only if your proxy sets it.

Does a trailing slash matter in a WordPress redirect?

Yes. WordPress's canonical redirect adds a trailing slash to most permalinks, so pointing a 301 at a slashless URL produces two hops instead of one. Every hop costs the visitor a round trip, and Google's crawlers follow at most 10. Match the exact final URL that WordPress serves, including the slash.

How do I test an .htaccess redirect without breaking my site?

Run curl with the head-only flag against the old URL and read the status code and Location header before trusting a browser. Browsers can cache a 301 and show you a stale result. Keep a copy of the working file first, since a bad line in .htaccess returns a 500 on every page including wp-admin.

Why did my whole site return a 500 error after adding a redirect?

A single bad line in .htaccess takes down every URL under that directory, admin included. Common causes are an unclosed IfModule wrapper, a typo in a directive name, or an old Order or Deny line on a server without mod_access_compat. Remove the last block you added and reload to confirm, then read the error log.