---
title: "Set up 301 redirects in WordPress: server, plugin or code · CloudSat"
description: "Decide where WordPress redirects should live (Nginx, Apache, a plugin or code), choose the right status code, and test for chains and loops before launch."
url: https://cloudsat.io/learn/set-up-301-redirects-in-wordpress
updated: 2026-11-18T12:00:00Z
source: CloudSat (https://cloudsat.io/). Prices in USD.
---

[Self-hosting](https://cloudsat.io/learn/topic/self-hosting)

# Set up 301 redirects in WordPress: server, plugin or code

Decide where WordPress redirects should live (Nginx, Apache, a plugin or code), choose the right status code, and test for chains and loops before launch.

CloudSat team November 18, 2026 6 min read

A redirect is a promise that an old address still leads somewhere useful. Broken promises show up as 404 pages in your analytics, dead links in old newsletters, and search results that point nowhere. Doing redirects well in WordPress comes down to three decisions: which status code to send, where the rule should live, and how you will test it before visitors do.

## Pick the status code first

| Code | Meaning | Use it for |
| --- | --- | --- |
| 301 | Moved permanently | Renamed pages, merged posts, a new domain |
| 308 | Permanent, request method kept | Permanent moves of forms or API endpoints that receive POST requests |
| 302 | Found, temporary | Short campaigns, maintenance pages |
| 307 | Temporary, request method kept | Temporary moves of POST endpoints |
| 410 | Gone | Content removed with no replacement |

Google treats 301 and 308 as permanent, which makes the target the canonical URL, and 302, 303 and 307 as temporary, which keeps the old URL in results. An instant meta refresh counts as permanent, a delayed one as temporary, and JavaScript redirects only as a last resort, because Google may never render them.

Two practical rules follow. Use a permanent code only when you are sure the move will not be reverted, because browsers cache permanent redirects and keep following them even after you delete the rule. And send removed content to a real equivalent or answer with 410. A redirect to an unrelated page, such as the home page, helps nobody and search engines may treat it like a missing page.

## What WordPress already does for you

- **Changed post slugs.** When you edit a published post's slug, WordPress stores the old one and redirects to the new URL with a 301. Nothing to configure.
- **Guessing on 404s.** When a URL is not found, WordPress tries to guess the intended post from its slug and redirects there. It helps after a permalink structure change, but it can guess wrong when slugs are similar. Developers can turn it off with the `do_redirect_guess_404_permalink` filter.

Everything else needs a rule: deleted or merged content, moved sections, renamed categories, and domain or protocol changes.

## Decide where the rule lives

| Location | Runs | Who edits it | Best for |
| --- | --- | --- | --- |
| Web server (Nginx, Apache, Caddy) | Before PHP and WordPress load | Server admins | Domain moves, HTTPS and www rules, large pattern-based moves |
| Plugin such as Redirection | Inside WordPress (or written to `.htaccess` on Apache) | Editors | Day-to-day content redirects, 404 monitoring |
| Code in a must-use plugin | Inside WordPress | Developers | Redirects that depend on logic or data |

A sensible split: anything about domains, protocols or whole sections at the server, and individual content redirects in a plugin that editors can manage without a deploy.

## Server rules

### Nginx

A domain move gets its own server block, and `$request_uri` carries the path and query string across:

```nginx
server {
    listen 443 ssl;
    server_name old-example.com www.old-example.com;
    ssl_certificate     /etc/letsencrypt/live/old-example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/old-example.com/privkey.pem;
    return 301 https://example.com$request_uri;
}
```

Inside the main site's server block, exact matches and patterns look like this:

```nginx
location = /pricing-2025/ { return 301 /pricing/; }
rewrite ^/blog/(.*)$ /news/$1 permanent;
```

For hundreds of one-to-one redirects, load them from a `map` file instead of writing a `location` for each.

### Apache

In `.htaccess`, put the rules above the block WordPress manages (between `BEGIN WordPress` and `END WordPress`), or WordPress's own rewrite catches the request first. Using `RewriteRule` for everything keeps the order predictable:

```apache
RewriteEngine On
RewriteRule ^pricing-2025/?$ /pricing/ [R=301,L]
RewriteRule ^blog/(.*)$ /news/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?old-example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
```

In `.htaccess` the pattern is matched without the leading slash, which is why the rules start with `pricing-2025`, not `/pricing-2025`.

### Caddy

```caddyfile
old-example.com, www.old-example.com {
    redir https://example.com{uri} permanent
}
```

On a CloudSat [WordPress server](https://cloudsat.io/wordpress-server-hosting) you have root access, so server-level rules are always available to you.

## Plugin rules with Redirection

The free Redirection plugin covers what editors need: 301 redirects managed from the dashboard, full regular expression support, conditions on login status, role, browser, referrer, cookies, headers or IP address, a 404 log that shows which old URLs people still request, and import and export as CSV, `.htaccess`, Nginx rules or JSON. On Apache it can write its redirects into `.htaccess` so Apache handles them before WordPress loads, and WP-CLI commands automate imports. Two server-specific notes: on Nginx, the plugin's own redirects work but `.htaccess` output is ignored, so export Nginx rules instead; on OpenLiteSpeed, restart the server after the plugin changes `.htaccess`.

## Redirects in code

For logic that a rule table cannot express, a must-use plugin is enough. Use `wp_safe_redirect()`, which only allows destinations on your own host, pass the status explicitly (the default is 302), and call `exit` afterward, since the function does not stop execution:

```php
<?php
// wp-content/mu-plugins/legacy-docs-redirect.php
add_action( 'template_redirect', function () {
    if ( is_404() && str_starts_with( $_SERVER['REQUEST_URI'], '/docs/v1/' ) ) {
        wp_safe_redirect( home_url( '/docs/' ), 301 );
        exit;
    }
} );
```

## Build the redirect map before you move anything

Redirects for a redesign or a domain move start as a spreadsheet, not as rules. List every URL that matters: the old XML sitemap, the pages that earn traffic in your analytics, and the URLs other sites link to, which Google Search Console shows under its links report. Give each one a destination, then look for patterns. Twenty posts that moved from `/blog/` to `/news/` become one rule, while the leftovers stay one-to-one.

Keep that file in version control next to the site's configuration. It doubles as the test list below, and it is the record you will want a year later when someone asks why an old URL lands where it does. For a full domain move, also use the Change of Address tool in Google Search Console once the redirects are live.

## Test before you launch

Check one URL and its full chain with curl. A good redirect takes one hop:

```bash
curl -sIL -o /dev/null -w '%{http_code} %{url_effective} hops=%{num_redirects}\n' http://old-example.com/blog/hello/
```

For a migration, keep the old and new URLs in a CSV file and test every line:

```bash
while IFS=, read -r old new; do
  result=$(curl -s -o /dev/null -w '%{http_code} %{redirect_url}' "$old")
  if [ "$result" = "301 $new" ]; then echo "OK   $old"; else echo "FAIL $old -> $result"; fi
done < redirects.csv
```

Run the loop against a staging copy first, again right after launch, and once more a week later, after caches and plugins have settled. Look for three failures: chains (old to older to new), loops (two rules pointing at each other), and HTTP to HTTPS hops stacked on top of a domain move. Fix chains by pointing every old URL straight at its final destination.

After a domain move, also update links inside your own content so they stop depending on redirects. WP-CLI does it in one pass; run it with `--dry-run` first and leave post GUIDs alone:

```bash
wp search-replace 'https://old-example.com' 'https://example.com' --skip-columns=guid --dry-run
```

## FAQ

### How long should a 301 redirect stay in place?

Google sets no expiry. Keep redirects as long as anyone might still follow the old links, which in practice means indefinitely: a rule costs almost nothing, and a broken old link costs a visitor.

### Is a plugin slower than a server rule?

A plugin redirect loads WordPress before answering, while a server rule answers immediately. For a few dozen redirects the difference is small. For thousands, or for whole-domain moves, use server rules.

### Should I use 301 or 308?

For normal pages either works, and 301 is the common choice. Use 308 when the old URL receives form submissions or API calls, because it tells clients to repeat the same method, such as POST, at the new address.

**Run it on CloudSat.** Cloud VPS on AMD EPYC and NVMe from $1.98/mo in 13 regions, delivered in under 5 minutes and paid in crypto. [See plans](https://cloudsat.io/plans) or [deploy a server](https://dash.cloudsat.io/console/deploy).

## Keep reading

[All guides in Self-hosting](https://cloudsat.io/learn/topic/self-hosting)

- [Apache, Nginx or OpenLiteSpeed for a WordPress server](https://cloudsat.io/learn/wordpress-web-server-apache-nginx-openlitespeed): DevOps Apache, Nginx or OpenLiteSpeed for a WordPress server 6 min read
- [How to choose an open-source CMS you can self-host](https://cloudsat.io/learn/choose-an-open-source-cms): Self-hosting How to choose an open-source CMS you can self-host 6 min read
- [Flat-file CMS or WordPress: when to skip the database](https://cloudsat.io/learn/flat-file-cms-vs-wordpress): Self-hosting Flat-file CMS or WordPress: when to skip the database 6 min read
- [Host several websites on one VPS with Nginx, Apache or IIS](https://cloudsat.io/learn/host-several-websites-on-one-vps): Self-hosting Host several websites on one VPS with Nginx, Apache or IIS 7 min read
