Back to Blog

How to Create 301 Redirects in WordPress (5 Methods)

A 301 redirect tells browsers and search engines that a page has permanently moved to a new URL. Whether you're changing permalinks, consolidating content, or managing affiliate links, here are five ways to implement 301 redirects in WordPress.

Key Takeaways

  • 301 redirects are permanent and pass SEO value to the new URL
  • .htaccess is fastest but requires server access and careful syntax
  • Plugins offer the easiest setup but add overhead
  • PHP redirects work but execute after WordPress loads
  • Always test redirects after implementation to avoid loops or chains

What is a 301 Redirect?

HTTP status code 301 means "Moved Permanently." When a browser or search engine requests a URL with a 301 redirect:

  1. The server responds with status 301 and a Location header
  2. The browser follows the Location to the new URL
  3. Search engines update their index to reflect the change
  4. Link equity transfers to the destination URL

When to Use 301 Redirects

Scenario Redirect Type Why
Changed permalink structure 301 Old URLs should point to new structure
Merged two posts into one 301 Consolidate link equity
Moved to new domain 301 Preserve SEO during migration
Affiliate link management 301 Clean URLs, easy updates
HTTP to HTTPS migration 301 Secure version is permanent
Temporary maintenance page 302 Will revert (not 301)

Method 1: .htaccess (Apache Servers)

The fastest method because it executes before WordPress loads. Requires Apache server with mod_rewrite enabled.

Single URL Redirect

# Add to .htaccess (before WordPress rules)
Redirect 301 /old-page https://yoursite.com/new-page

Multiple Redirects with RewriteRule

RewriteEngine On

# Redirect single page
RewriteRule ^old-slug/?$ /new-slug/ [R=301,L]

# Redirect entire directory
RewriteRule ^old-category/(.*)$ /new-category/$1 [R=301,L]

# Redirect with query string
RewriteCond %{QUERY_STRING} ^id=123$
RewriteRule ^page\.php$ /new-page/? [R=301,L]

HTTP to HTTPS

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Warning: Syntax Errors Break Your Site

A single typo in .htaccess can cause a 500 error and lock you out of WordPress. Always backup the file first and test access immediately after changes.

Method 2: Nginx Configuration

If your server runs Nginx instead of Apache, .htaccess won't work. Add redirects to your server block:

server {
    # Single redirect
    location = /old-page {
        return 301 https://yoursite.com/new-page;
    }

    # Redirect with regex
    location ~ ^/old-category/(.*)$ {
        return 301 https://yoursite.com/new-category/$1;
    }

    # HTTP to HTTPS
    if ($scheme = http) {
        return 301 https://$host$request_uri;
    }
}

After editing, reload Nginx:

sudo nginx -t && sudo systemctl reload nginx

Method 3: PHP in functions.php

Executes within WordPress, so it's slower than server-level redirects but doesn't require server access.

// Add to functions.php or a custom plugin

add_action('template_redirect', 'custom_301_redirects');
function custom_301_redirects() {
    // Get current path
    $request = $_SERVER['REQUEST_URI'];

    // Define redirects
    $redirects = array(
        '/old-page/'     => '/new-page/',
        '/old-post/'     => '/updated-post/',
        '/category/old/' => '/category/new/',
    );

    // Check and redirect
    foreach ($redirects as $old => $new) {
        if (strpos($request, $old) === 0) {
            wp_redirect(home_url($new), 301);
            exit;
        }
    }
}

Redirect Deleted Posts to Category

add_action('template_redirect', 'redirect_deleted_to_category');
function redirect_deleted_to_category() {
    if (is_404()) {
        // Redirect all 404s to blog page
        wp_redirect(home_url('/blog/'), 301);
        exit;
    }
}

Method 4: WordPress Redirect Plugin

The easiest method for non-developers. Popular options include:

Plugin Free/Paid Best For Downsides
Redirection Free General redirect management Complex interface, database bloat
301 Redirects Free Simple URL mapping Limited features
Yoast Premium Paid SEO users already using Yoast Requires premium subscription
External Redirect Pro $19 Post-level redirects, affiliate links Per-post only, not URL patterns

For post-level redirects (turning WordPress posts into affiliate links or external redirects), External Redirect Pro adds a simple checkbox to the post editor. No complex interfaces or database tables.

Method 5: Database-Level Redirects

For advanced users: store redirects in a custom database table and check on each request. This scales better than hardcoded arrays for thousands of redirects.

// Check custom table for redirects
add_action('template_redirect', 'db_redirect_check');
function db_redirect_check() {
    global $wpdb;
    $request = $_SERVER['REQUEST_URI'];

    $table = $wpdb->prefix . 'custom_redirects';
    $redirect = $wpdb->get_row(
        $wpdb->prepare(
            "SELECT new_url FROM {$table} WHERE old_url = %s AND active = 1",
            $request
        )
    );

    if ($redirect) {
        wp_redirect($redirect->new_url, 301);
        exit;
    }
}

Testing Your Redirects

Always verify redirects work correctly after implementation:

Browser DevTools

  1. Open DevTools (F12)
  2. Go to Network tab
  3. Check "Preserve log"
  4. Visit the old URL
  5. Look for 301 status and correct Location header

Command Line

curl -I https://yoursite.com/old-page

# Expected output:
HTTP/2 301
location: https://yoursite.com/new-page

Online Tools

Services like httpstatus.io show the complete redirect chain and final destination.

Common 301 Redirect Problems

Redirect Loops

Page A redirects to B, B redirects to A. Results in "too many redirects" error.

Fix: Map out all redirects and ensure no circular references.

Redirect Chains

A redirects to B, B redirects to C, C redirects to D. Each hop adds latency.

Fix: Update A to redirect directly to D.

Mixed Content After HTTPS Redirect

Page loads over HTTPS but contains HTTP resources.

Fix: Update internal links and use plugins like Really Simple SSL.

Caching Issues

Browser caches 301 redirect even after you remove it.

Fix: Clear browser cache or test in incognito mode.

Need Post-Level 301 Redirects?

Method Best For Complexity
.htaccess Server-level, bulk redirects High (syntax-sensitive)
PHP code Developers, custom logic Medium
Redirect plugins URL pattern management Low
External Redirect Pro Affiliate links, external redirects Very Low (checkbox)

If your use case is post-level redirects: Turning posts into affiliate links, sending traffic to external resources, or managing trackable URLs. External Redirect Pro adds a checkbox to the post editor with admin bypass.

One-time payment. No subscriptions. Lifetime updates.

Get External Redirect Pro - $19

Summary

Choose your redirect method based on your needs: .htaccess for speed and server access, Nginx for Nginx servers, PHP for WordPress-level control, plugins for ease of use, or database tables for scale. Always test after implementation and watch for loops or chains.

H

Haohunter

WordPress developer building lightweight plugins that solve real problems. No bloat, no subscriptions, just tools that work.