301 redirects: .htaccess, Nginx, WordPress and Cloudflare, with code

Kevin Seeberger, · 9 min read

A 301 redirect tells browsers and search engines that a URL has moved permanently and where it lives now. According to Google’s documentation, Google uses a 301 as a signal that the redirect target should be canonical and shows the target, not the old URL, in search results. You set it up on the server: in .htaccess on Apache, with return 301 on Nginx, through a plugin or PHP on WordPress, or with a Redirect Rule on Cloudflare. The code for all four is below.

Most guides on this show one RewriteRule and stop. The mistakes I actually find in audits sit in the defaults: Apache, WordPress and Nginx send a 302 unless you say otherwise, Cloudflare drops the query string by default, and an .htaccess rule that starts with a slash never matches. Add chains left over from several relaunches. I checked every source on 19 November 2026; Google’s redirects page was last updated on 16 November 2026.

301, 302, 307 or 308: which status code when

Google splits redirects into two groups. With permanent redirects (301, 308), Googlebot follows the redirect and the indexing pipeline uses it as a signal that the target should be canonical. With temporary redirects (302, 303, 307), Googlebot follows too, but the redirect isn’t used as a canonicalization signal. The target can still be indexed if other signals point to it, Google says.

Google’s HTTP status code reference is more direct: a 301 is a strong signal that the target should be processed, a 302 a weak one. 307 is treated like 302, 308 like 301.

Code Meaning Google Use it when
301 moved permanently strong signal for the target a URL moves for good: relaunch, domain change, merged pages
308 permanent, method preserved same as 301 the same, when POST requests (forms, APIs) must stay POST
302 temporary weak signal, old URL usually stays indexed a page points elsewhere briefly, such as during maintenance
307 temporary, method preserved same as 302 like 302, with POST

The difference between 301 and 308 isn’t about Google, it’s in the HTTP standard. RFC 9110 allows browsers, for historical reasons, to turn a POST into a GET after a 301; a 308 forbids that. For ordinary pages it doesn’t matter, so use 301. Google still asks you to use the semantically correct code so other clients benefit from it.

My decision rule: if the old URL is never coming back, it’s a 301. If you’re not sure, it’s still almost always a 301, because “temporary” rarely stays temporary. I only use a 302 when there is a dated plan to revert.

If there is no fitting replacement at all, a redirect is the wrong answer. For removed pages with no replacement that has similar content, Google recommends returning 404 or 410. Sending everything to the home page helps neither users nor Google.

301 redirect in .htaccess (Apache)

Apache gives you two modules. mod_alias with Redirect and RedirectMatch is enough for single URLs. You need mod_rewrite for conditions such as hostname or HTTPS.

A single page with Redirect

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

Two things in the Apache documentation that people miss:

  • Without a status, it’s a 302. Redirect "/a" "/b" is a temporary redirect. Always write 301 or permanent.
  • Redirect matches the start of the path and appends whatever follows to the target. Redirect 301 "/shop" "https://www.example.com/store" also sends /shop/shoes/ to /store/shoes/. Often that’s what you want. If not, use RedirectMatch with a regular expression that matches exactly one URL:
RedirectMatch 301 "^/old-page/?$" "https://www.example.com/new-page/"

For a page that is gone with no replacement, the same directive can send a 410 without a target:

Redirect gone "/promo-2024/"

Domain change and HTTPS with mod_rewrite

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

The most common trap: in .htaccess, RewriteRule sees the path without a leading slash. The Apache documentation states that a pattern starting with ^/ will never match in this context, and it fails silently. That’s why the rule above uses ^(.*)$ and /$1 in the target. In the server or VirtualHost config it’s the other way round: the path starts with a slash there.

Also: [R] without a number gives a 302, according to Apache, so always write R=301. Apache recommends combining [R] with [L] almost every time. mod_rewrite passes the query string through unchanged by default, so ?utm_source=… survives.

http to https and non-www to www in one rule, so there’s no intermediate hop:

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

%{REQUEST_URI} holds the path with its slash but without the query string, which is appended automatically as above. If the site sits behind a proxy or CDN that terminates HTTPS, %{HTTPS} can always be off on the origin. That causes a loop, and the check belongs on the proxy.

301 redirect in Nginx

Nginx has no .htaccess. Redirects live in the server configuration, most simply with return, which the Nginx documentation allows with 301, 302, 303, 307 and 308.

A single page:

location = /old-page/ {
    return 301 https://www.example.com/new-page/;
}

A whole domain move, keeping path and query string:

server {
    listen 80;
    listen 443 ssl;
    server_name old-domain.com www.old-domain.com;
    # ssl_certificate and ssl_certificate_key as usual

    return 301 https://www.new-domain.com$request_uri;
}

$request_uri is, per Nginx, the full original request URI with arguments. The old domain still needs a valid certificate, otherwise visitors on https get a certificate warning instead of the redirect.

Moving a directory with a pattern:

rewrite ^/blog/(.*)$ /articles/$1 permanent;

permanent sends a 301, redirect a 302. One detail if you copy Google’s example: its Nginx lines are missing the semicolons. Nginx needs them, every simple directive ends with ;. Test the config with nginx -t before reloading.

301 redirect in WordPress

WordPress gives you three routes, and you’re already using one of them.

WordPress itself. When you change a post’s slug, WordPress remembers the old one and sends a 301 through wp_old_slug_redirect(). That covers changed post slugs, not a changed permalink structure or deleted pages.

A plugin. Redirection manages redirects through WordPress by default, logs 404s and can export its rules as .htaccess or Nginx rewrite rules. Some SEO plugins ship their own redirect managers. Use only one, or you won’t know which rule fires.

Your own code. Without a plugin, hook into template_redirect:

add_action( 'template_redirect', function () {
    $path = trim( (string) wp_parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ), '/' );

    if ( 'old-page' === $path ) {
        wp_safe_redirect( home_url( '/new-page/' ), 301 );
        exit;
    }
} );

The trap: wp_redirect() and wp_safe_redirect() send a 302 unless you pass the status. Neither stops the script, hence the exit.

For a domain change or hundreds of rules from a redirect map, the redirects don’t belong in PHP but on the server (Apache, Nginx) or at Cloudflare. Then WordPress doesn’t have to boot for an old URL at all.

301 redirect on Cloudflare

If your domain runs through Cloudflare, you can redirect without server access. Cloudflare offers two products for this: Single Redirects (called “Redirect Rules” in the dashboard) per domain, and Bulk Redirects for long lists across the account. Both only work when the DNS record is proxied through Cloudflare (orange cloud).

A Redirect Rule for a domain move, in the dashboard under Rules, Create rule, Redirect Rule, type “Wildcard pattern”:

Request URL:            http*://old-domain.com/*
Target URL:             https://www.new-domain.com/${2}
Status code:            301
Preserve query string:  on

${1} stands for the first asterisk (empty or “s” from http*), ${2} for the path. Create a second rule for www.old-domain.com.

The trap: per the Cloudflare documentation, 301 is the default status, but “Preserve query string” is off by default. Without it, every redirected URL loses its parameters, campaign tags included.

On limits: as of November 2026, Cloudflare allows 10 Redirect Rules per domain on the Free plan, without regular expressions. Bulk Redirects handle up to 10,000 URL redirects per account on Free. For a relaunch map with hundreds of individual URLs, Bulk Redirects is the right tool, not twenty wildcard rules.

Avoiding redirect chains and loops

A chain is A pointing to B and B pointing to C. The classic case comes after the second relaunch: old rules stay, new ones are added, and http://example.com/product runs through https, then www, then the old path, then the new one.

Googlebot follows up to 10 redirect hops, according to Google. That doesn’t make ten acceptable. In its site move guide, Google advises redirecting to the final destination directly, and if that’s not possible, keeping chains short: ideally no more than 3 and fewer than 5. Every hop adds latency, and not all browsers support long chains.

How I keep chains short:

  • With every new redirect, check whether existing rules already point to the old source, and point those straight at the new target.
  • Fix protocol and hostname in one step (see the .htaccess example with [OR]).
  • Change internal links to the target URL. A redirect is for external links and bookmarks, not your own navigation.

Loops usually come from two layers working against each other: Cloudflare redirects to www, the server back to non-www. Or a plugin adds a trailing slash and the server removes it. Decide on one layer per rule.

How long to keep 301 redirects

As long as possible, generally at least one year, Google says. That gives Google time to transfer all signals to the new URLs, including links from other sites. From the users’ perspective, Google suggests keeping redirects indefinitely.

My default: redirects for URLs with external links, traffic or bookmarks stay permanently. Removing one is only worth it for rules that demonstrably never get hit, and only once server logs have shown that over months.

One thing about testing in a browser: per RFC 9110, a 301 is heuristically cacheable when no cache headers say otherwise. When you fix a wrong 301, your browser may still show the old one. So don’t test in a normal browser window; use curl -sIL https://example.com/old-page/ or a tool that sends a fresh request.

Check every redirect

After setting up, check three things: is the status really 301 and not 302? Does the URL reach the target in one hop? Does the target answer 200, not 404 or noindex?

That’s what the Redirect Checker shows: every hop from the URL you enter to the final page with status code and timing, chains and loops, the http/https and www variants, and the headers on the target, including X-Robots-Tag. Enter the old URL with a parameter such as ?test=1 as well, and you see straight away whether the query string survives.

In Search Console, the Page indexing report lists the URLs Google knows as redirects under “Page with redirect”. The URL Inspection tool shows which page Google chose as canonical for a single URL.

In a relaunch, the redirect map is half the work

In a relaunch, a handful of rules quickly becomes hundreds. The redirects themselves are then the easy part. The hard part is the mapping: which old URL gets which new target, which gets a 410, and which chains from earlier moves need resolving. I wrote about why fix lists without implementation go nowhere in Why most SEO audits end up in a drawer. A redirect map is no different.

On launch day, robots.txt belongs on the list next to the redirects. Google’s site move guide points out that some site owners block all crawling during development and that the robots.txt for the move needs to be ready. How robots.txt rules actually behave is covered in my piece on AI crawler controls and robots.txt.

More articles on crawling, indexing and relaunches are under technical SEO. If you’re planning a relaunch or domain move and don’t want to build the redirect map alone, the relaunch and migration page explains how I work and what it costs.