Guide · PHP & platforms

PHP, WordPress & platform redirects

Can’t touch the server config? You can still send a proper 301 from PHP, your CMS or your hosting platform. Here’s how to do it right on each — and when an HTML redirect is an acceptable last resort.

PHP header()

The 2014 version of this site had one PHP snippet, and it still works. The modern version is the same idea with the status code passed explicitly:

old-page.php
<?php
header('Location: https://example.com/new-page', true, 301);
exit;
  • It must run before any output. Even a blank line or a UTF-8 BOM before <?php triggers “Cannot modify header information – headers already sent”.
  • Always exit. header() only sets a header; without exit, the rest of the page still runs (and may output things, or do things).
  • Pass the code. header('Location: …') on its own sends a 302, not a 301.
  • Never redirect to unchecked user input like $_GET['url'] — that’s an “open redirect” that spammers and phishers love.

PHP: a list of redirects

For sites where every request goes through one index.php (most CMSs and frameworks), a lookup table near the top works well:

index.php
<?php
$redirects = [
    '/about-us.html'    => '/about',
    '/services.php'     => '/what-we-do',
    '/product.php?id=7' => '/shop/blue-widget',
];

$uri  = $_SERVER['REQUEST_URI'];
$path = parse_url($uri, PHP_URL_PATH);
$to   = $redirects[$uri] ?? $redirects[$path] ?? null;

if ($to !== null) {
    header('Location: ' . $to, true, 301);
    exit;
}

PHP only sees requests that reach PHP. Images, PDFs and other static files are usually served directly by the web server, so they won’t be redirected. For those — and for whole-domain moves — use .htaccess or Nginx rules.

WordPress

You have three good options, from least to most technical:

  1. A redirect plugin. Friendly admin screen, CSV import, and many log 404s so you can spot URLs that need redirecting. Great for editors. Make sure it’s set to send 301s.
  2. .htaccess rules above the # BEGIN WordPress block. Fastest option, and they cover images and uploads too. See the cookbook.
  3. Code in a small plugin or your theme’s functions.php:
functions.php
<?php
add_action('template_redirect', function () {
    $redirects = [
        '/old-page/'  => '/new-page/',
        '/2019/hello/' => '/news/hello/',
    ];
    $path = wp_parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
    if (isset($redirects[$path])) {
        wp_safe_redirect(home_url($redirects[$path]), 301);
        exit;
    }
});

wp_safe_redirect() refuses to send people to other domains unless they’re allow-listed, which protects against open redirects. For redirects to another domain, use wp_redirect() with a hard-coded URL.

Moving WordPress to a new domain? Change the Site Address in Settings → General, search-and-replace the old domain in the database (WP-CLI’s wp search-replace handles serialised data safely), then add the whole-domain redirect to the old domain’s .htaccess.

Laravel

routes/web.php
// routes/web.php
Route::permanentRedirect('/old-page', '/new-page');        // 301
Route::redirect('/promo', '/summer-sale');                   // 302
Route::redirect('/api/v1/orders', '/api/v2/orders', 308);    // any code

Netlify & Cloudflare Pages

Both read a plain-text _redirects file from your published folder. One rule per line: from, to, status.

_redirects
/old-page        /new-page         301
/blog/*          /news/:splat      301
/docs            https://docs.example.com  301

On Netlify, a rule only applies when no file exists at the old path; add ! (301!) to force it. Netlify can also do whole-domain redirects (https://old.com/* https://new.com/:splat 301!) once the old domain is added to the site. Cloudflare Pages can’t match domains in _redirects — use Cloudflare’s Redirect Rules for that.

Vercel

vercel.json
{
  "redirects": [
    { "source": "/old-page", "destination": "/new-page", "permanent": true },
    { "source": "/blog/:slug*", "destination": "/news/:slug*", "permanent": true },
    { "source": "/promo", "destination": "/summer-sale", "permanent": false }
  ]
}

Note that Vercel’s "permanent": true sends a 308, and false sends a 307. Search engines treat those the same as 301 and 302.

Cloudflare rules

If your domain is on Cloudflare, you can redirect at the edge before requests ever reach your server — handy when the old site has no hosting left at all. In the dashboard:

  • Rules → Redirect Rules for patterns (e.g. “hostname equals old.com → concatenate https://new.com + path, keep query string, 301”).
  • Bulk Redirects for a long list of one-to-one URLs, uploaded as a CSV.
  • The old domain needs a proxied (orange-cloud) DNS record for the rule to run.

Meta refresh & JavaScript

When you truly can’t send a server-side status code — some static hosts, a page builder with no redirect settings — an HTML redirect is better than nothing:

old-page.html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>This page has moved</title>
  <link rel="canonical" href="https://example.com/new-page">
  <meta http-equiv="refresh" content="0; url=https://example.com/new-page">
  <script>location.replace("https://example.com/new-page");</script>
</head>
<body>
  <p>This page has moved to <a href="https://example.com/new-page">example.com/new-page</a>.</p>
</body>
</html>

An instant (0-second) meta refresh is interpreted by Google much like a permanent redirect; JavaScript redirects work once Google renders the page. Both are slower for visitors and weaker signals than a real 301, so switch to a server-side redirect as soon as you can.