YepAPI

How to Do a 301 Redirect

Learn how to do a 301 redirect correctly, with WordPress, Apache, Nginx, PHP, testing steps, and migration advice.

YepAPI TeamEngineering & Product
9 min read

How to do a 301 redirect: create a server-side rule that returns HTTP 301 and a new Location header, point the old URL to the most relevant new URL, then test the response and final destination. Google treats 301 as a permanent move, so use it for lasting URL changes, migrations, and content consolidation.

Unicorn operating a redirect gate for how to do a 301 redirect, sending one old URL tile to one final page

A 301 redirect tells browsers and crawlers that a URL has moved permanently. The server returns a 301 response and a Location header with the destination URL. If you need a refresher on the definition, see what is 301 redirect.

Google recommends using permanent redirects when content has moved for good, as explained in Google Search Central's redirect guidance. That matters for SEO because redirects help users and crawlers reach the right page instead of a dead one.

When to use a 301 redirect#

Use a 301 redirect when the old URL should never be served again and there is a clear replacement.

Common cases include:

  1. Moving a page to a new URL.
  2. Merging duplicate or overlapping pages.
  3. Changing site structure during a migration.
  4. Consolidating HTTP to HTTPS.
  5. Standardizing www and non-www versions.
  6. Retiring a product or article that has a close substitute.

Do not use a 301 just because a page performs poorly. If the page is intentionally gone with no replacement, a 404 or 410 can be more honest. If multiple similar URLs should remain accessible but one should be treated as primary, a canonical tag may be the better fit.

301 vs 302 redirect#

A 301 means permanent. A 302 means temporary. That is the core difference in the 301 redirect vs 302 decision.

SituationUse 301Use 302
URL moved for goodYesNo
Short-term test or maintenanceNoYes
Site migrationYesNo
Temporary campaign landing pageNoYes
Protocol or host canonicalizationYesNo

Google can sometimes treat long-lived 302s like permanent moves, but that is not a reason to be vague. Use the status code that matches your intent. Mozilla's HTTP response status code reference documents 301 as a permanent redirect.

How to do a 301 redirect in WordPress#

The safest way to handle how to set up a 301 redirect in WordPress depends on your stack.

Option 1: Use your host or edge platform#

If your site runs behind managed hosting, a reverse proxy, or a CDN rule engine, set the redirect there first. Platform-level rules usually run before WordPress boots, which makes them faster and easier to audit.

Option 2: Use a redirect plugin carefully#

A plugin can work for a small number of page-level redirects. Keep the map simple. Avoid stacking plugin redirects on top of server rules, because mixed layers are a common cause of chains and loops.

Option 3: Add a server rule for WordPress on Apache#

If WordPress runs on Apache and allows overrides, add a rule to .htaccess above the main WordPress rewrite block:

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

For pattern-based moves, use mod_rewrite:

APACHE
RewriteEngine On
RewriteRule ^old-category/(.*)$ https://example.com/new-category/$1 [R=301,L]

After saving the file, request the old URL and confirm the server returns 301, not 302 or 200.

How to do a 301 redirect on Apache, Nginx, PHP, and CDN rules#

This is where most guides stop too early. Developers usually need the exact method for the stack they actually run.

Apache virtual host config#

If you manage Apache config directly, place redirects in the site config instead of .htaccess when possible.

APACHE

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

Server-level rules are easier to version, review, and deploy at scale.

Nginx#

In Nginx, use return 301 for simple one-to-one redirects:

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

For host or protocol consolidation:

NGINX
server {
  listen 80;
  server_name example.com www.example.com;
  return 301 https://www.example.com$request_uri;
}

Nginx rules are explicit and fast. They are also easy to misuse if overlapping location blocks conflict, so test every rule after deploy.

PHP#

If you must redirect inside application code, send the 301 status and Location header before output:

PHP
<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: https://example.com/new-page/");
exit;

App-level redirects are usually slower than web-server or CDN rules. Use them when the redirect logic depends on application data.

CDN or edge rules#

Many platforms let you define path or hostname redirects at the edge. That is often the best choice for HTTPS, www, trailing slash, locale, and migration rules because the response is returned before the request reaches origin.

The important part is not the interface. The important part is that the response is truly server-side, returns 301, and sends users directly to the final URL in one hop.

How to do a 301 redirect during a migration#

Large migrations fail when teams treat redirects as one-off fixes instead of a mapping problem.

Use this workflow:

  1. Export every current indexable URL.
  2. Map each old URL to the single best new URL.
  3. Keep topical relevance as tight as possible.
  4. Write pattern rules only where the pattern is truly consistent.
  5. Use one-to-one rules for exceptions.
  6. Update internal links, canonicals, sitemaps, and navigation.
  7. Test the full redirect list before launch.
  8. Crawl the old URL set again after launch.

Blanket-redirecting everything to the home page is a common mistake. It creates poor UX and weak relevance signals. During a full audit, YepAPI's On-Page Instant API can check large sets of old URLs for final status, canonical target, and response behavior without manual spot checks.

If you are planning the broader audit around a migration, pair the redirect map with a technical SEO site audit so you catch internal links, canonicals, and stale references at the same time.

How to test a 301 redirect#

Most redirect problems are not setup problems. They are testing problems. You think the rule works because the browser lands somewhere, but you never confirm the actual status code or hop path.

Use this checklist after every change:

  1. Request the old URL directly.
  2. Confirm the response status is 301.
  3. Confirm the Location header points to the intended destination.
  4. Follow the redirect and confirm the final URL returns 200.
  5. Check that there is only one hop where possible.
  6. Verify the final page is relevant to the original URL.
  7. Re-test with and without trailing slash, query string, and protocol variations.
  8. Check internal links so they point to the final URL, not the old one.

With curl, you can inspect headers fast. The curl documentation for HTTP headers shows how to fetch response headers and follow redirects.

Examples:

Terminal
curl -I https://example.com/old-page/
Terminal
curl -I -L https://example.com/old-page/

You can also test in Postman or your browser's network panel. The goal is the same: verify the status code, the Location header, and the final destination.

Redirect chains and loops#

Redirect chains and loops waste time for users and crawlers. They also make migrations harder to debug.

A redirect chain looks like this:

  • /old-a → /old-b
  • /old-b → /new-c

A redirect loop looks like this:

  • /page-a → /page-b
  • /page-b → /page-a

Keep redirects to one hop whenever you can. If a page already redirects, update the original rule to point straight to the final destination. If you want a deeper breakdown, see redirect chain.

Redirect cleanup also supports crawl efficiency. Google's crawler still has to request each hop, which can add friction on large sites. That is one reason redirect hygiene belongs in technical SEO best practices and in any discussion of crawl budget in SEO.

301 redirect SEO: what helps and what hurts#

A 301 redirect can preserve discovery and relevance better than leaving a moved URL broken. It does not fix bad information architecture on its own.

Good outcomes usually come from:

  • Redirecting to the closest matching page.
  • Updating internal links after the move.
  • Keeping one canonical destination.
  • Removing outdated redirect hops.
  • Maintaining consistent protocol and host rules.

Bad outcomes usually come from:

  • Redirecting everything to the home page.
  • Pointing old product pages to unrelated categories.
  • Leaving internal links on the old URLs.
  • Mixing app, plugin, and server rules without a clear order.
  • Forgetting to test mobile, www, non-www, slash, and query-string variants.

For a page-level check, YepAPI's On-Page Content API helps you compare final-page elements after a redirect, and YepAPI's On-Page Instant API helps verify the live response path.

How to audit redirects programmatically at scale#

Manual checks work for five URLs. They break at five thousand.

A practical API-first workflow looks like this:

  1. Start with your legacy URL list.
  2. Request each old URL and capture the returned status code.
  3. Record the Location header when a redirect appears.
  4. Follow the chain to the final destination.
  5. Confirm the final page returns 200.
  6. Compare the final canonical and page signals.
  7. Flag chains, loops, soft 404s, and irrelevant targets.

YepAPI's On-Page Instant API is the fastest way to validate redirect behavior in code. YepAPI's On-Page Lighthouse API is useful when you also want to measure the performance impact of destination pages after a migration. If you need a wider technical stack, best on page SEO analysis tools covers where page auditing fits.

A simple decision table helps:

SituationBest action
Old URL has a clear replacement301 redirect
Page is temporarily unavailable302 redirect
Duplicates should stay liveCanonical tag
Page is gone with no substitute404 or 410
Internal path changed but old links remainUpdate links and add 301 if external users still hit the old URL

Common 301 redirect mistakes#

These are the errors that cause real losses in migrations and cleanup projects:

Redirecting to irrelevant pages#

Send users to the closest equivalent page, not the page you wish would rank. Relevance matters.

A redirect is a safety net, not the preferred route. Update internal links so the site points directly to the final URL.

Creating duplicate rule layers#

If the CDN redirects one way and the application redirects another, you can create hard-to-debug loops.

Forgetting canonical and sitemap updates#

Redirects do not replace housekeeping. Update canonicals, XML sitemaps, hreflang references, and navigation.

Ignoring 404s that should stay 404s#

Some URLs should remain gone. If you need the decision criteria, how to fix 404 error is the adjacent case to compare.

Post-launch checklist#

Use this after any redirect release, migration, or URL change:

  1. Export a sample of old URLs and test them.
  2. Confirm every intended move returns 301.
  3. Confirm the final URL returns 200.
  4. Remove chains and loops.
  5. Update internal links to final destinations.
  6. Update canonical tags and XML sitemaps.
  7. Watch crawl reports and server logs.
  8. Re-crawl the site after launch.
  9. Spot-check templates, faceted URLs, and campaign parameters.
  10. Keep the redirect map in version control.

YepAPI's SEO Audit API is the practical way to turn that checklist into a repeatable workflow across many URLs and releases.

CTA: audit redirects in code#

If you need more than browser spot checks, use YepAPI's On-Page Instant API to validate redirect status, final URLs, canonicals, and page responses at scale. You get $5 free credit and no card is required.

FAQ#

What is the code for a redirect?#

The code depends on the type of redirect. A 301 means the move is permanent. A 302 means the move is temporary. For SEO and site migrations, use a 301 only when the old URL has a lasting replacement and you want browsers and crawlers to request the new location instead.

Is a cname just a redirect?#

No. A CNAME is a DNS record that points one hostname to another hostname. A redirect is an HTTP response sent by a server or edge platform, usually with a status like 301 or 302 and a Location header. DNS changes routing. Redirects change the requested URL in the browser and crawler flow.

What does a 301 redirect do?#

A 301 redirect tells browsers and search engines that a URL has moved permanently. The server responds with HTTP 301 and a Location header for the new URL. Users are sent to the new page, and search engines can transfer the old URL's signals to the replacement when the move is relevant and consistent.

How to fix redirect error in WordPress?#

Start by checking for conflicting rules between your redirect plugin, .htaccess, hosting panel, and CDN settings. Then test the old URL's status code and Location header to find loops or chains. In WordPress, most redirect errors come from duplicate rule layers, wrong destination URLs, or forcing both plugin and server redirects at once.

Topics

301-vs-302-redirecthow-to-set-up-a-301-redirect-in-wordpresredirect-chains-and-loopshow-to-test-a-301-redirect301-redirect-seo

Start vibe coding with one API key.

One API key. 184 endpoints and 285 AI models. Yep, that's it.