PHP 301 Redirect Generator

Updated October 2026

Generate a correct PHP redirect with header() and the right 301 or 302 status code, for a single page or a whole list of URLs. Copy the code and paste it at the top of your PHP file.

Advertisement

Advertisement

Input Mode

The URL path you want to redirect from (e.g., /old-page). Rules match this exact path; add a second redirect for the trailing-slash version if both exist.

The destination URL (can be relative path or full URL)

301 redirects preserve SEO link equity and are permanent

The PHP 301 Redirect Code

A redirect in PHP is one function call. The important parts are the third argument, which sets the status code, and the exit that stops the rest of the script:

<?php
// Put this at the very top of the old page (/old-page), before any output.
header('Location: https://www.example.com/new-page', true, 301);
exit;

That is exactly what the generator writes for a single redirect. Leave out the 301 and PHP sends a 302, which search engines treat as temporary: the old URL stays indexed and the new one does not inherit its rankings.

Redirecting Many URLs in PHP

For a list of redirects, repeating header() calls does not work: the first one always wins. In bulk mode the generator writes a lookup table instead, which you include once at the top of your front controller (usually index.php):

<?php
$redirects = [
    '/old-page' => ['/new-page', 301],
    '/blog/post-1' => ['https://www.example.com/articles/post-1', 301],
    '/sale' => ['/offers', 302],
];

$path = rawurldecode((string) parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));

if (isset($redirects[$path])) {
    list($target, $status) = $redirects[$path];
    header('Location: ' . $target, true, $status);
    exit;
}

The generated version also matches old URLs that include a query string and passes the visitor's query string through to the new URL.

When to Use a PHP Redirect, and When Not To

SituationBest option
Shared hosting, no access to server config or .htaccessPHP redirect
One old script that moved (old-page.php)PHP redirect at the top of that file
Destination depends on logic (login state, language, database lookup)PHP redirect
Apache with .htaccess access.htaccess redirect
NginxNginx redirect
The old URL is a static file (.html, .pdf, an image)Server-level redirect; PHP never runs for static files

Common PHP Redirect Mistakes

  • Output before the header. Any character sent first, even a blank line above <?php, causes "headers already sent" and the redirect is ignored.
  • No status code. header('Location: ...') alone is a 302.
  • No exit. The script keeps running after the header is queued.
  • Relative destinations. Browsers accept them, but an absolute URL avoids surprises behind proxies and is what the HTTP specification originally required.
  • Redirect chains. Point each old URL straight at the final destination, not at another URL that redirects again.

Redirect Generators for Other Servers

The same tool is available preset for each server, plus a combined version:

After the redirects are live, confirm each old URL answers 301 and lands on a working page with the broken link checker, and submit a sitemap of the new URLs made with the XML sitemap generator.

How to Use

  1. 1

    PHP is already selected as the server type

  2. 2

    Enter the old path (for example /old-page) and the new URL

  3. 3

    Choose 301 for a permanent move or 302 for a temporary one

  4. 4

    Copy the generated PHP and place it at the very top of the old file, before any HTML or whitespace

  5. 5

    For many URLs, switch to Bulk Redirects and paste one "old new" pair per line

Why This Tool Matters for SEO

A PHP redirect is the right tool when you cannot edit the web server configuration: shared hosting without .htaccess access, a single legacy script that moved, or a destination that has to be decided in code. Sending the 301 status explicitly matters. Without it, PHP sends a 302, which tells search engines the move is temporary and keeps the old URL in the index.

Frequently Asked Questions

How do I do a 301 redirect in PHP?

Call header('Location: https://www.example.com/new-page', true, 301); followed by exit; before any output is sent. The third argument sets the HTTP status code; without it PHP sends a 302 temporary redirect.

Why do I get "headers already sent"?

Something was output before header() ran: a blank line or space before <?php, a UTF-8 byte order mark, an echo, or an included file that prints something. Move the redirect to the very first lines of the file and save the file without a BOM.

Is a PHP redirect as good for SEO as an .htaccess redirect?

Yes. Search engines only see the HTTP response, and a 301 sent by PHP is identical to a 301 sent by Apache or Nginx. The server-level redirect is slightly faster because PHP does not have to start, so prefer it when you have access.

Do I need exit after header()?

Yes. header() only queues the response header; the rest of the script keeps running unless you stop it. Without exit, code below the redirect still executes and may output content or perform actions you did not intend.

Related Tools You Might Like

More in Technical SEO tools · All free SEO tools

Local Processing: Your data never leaves your browser