Skip to content

Redirects: .htaccess, PHP or a plugin?

You change the addresses on your site - after a migration, a rebuild or a tidy-up of categories - and the old links have to lead to the new ones. There are three ways to do it: rules in an .htaccess file, a few lines of PHP, or a WordPress plugin. All of them work, but each is good at something different.

In short

  • .htaccess - the fastest, runs before PHP even starts. Good for fixed lists of addresses, long ones included.
  • PHP - when the addresses depend on data (from a database, say) or the server does not read .htaccess.
  • A plugin - convenient in the admin panel and, more importantly, it logs 404 errors.

Why redirect at all

An old address lives longer than you think: in Google's results, in bookmarks, in links from other sites, in old emails. Without a redirect, each of them ends in a 404 error, and over time Google drops the page from its results along with its ranking. A 301 redirect says "I have moved here" and carries the old address's value over to the new one. When to use a 301, a 410 or noindex is covered in 301, 410 and noindex redirects.

1. The .htaccess file

On an Apache server (and on hosts running LiteSpeed), redirects go into the .htaccess file:

RewriteEngine On
RewriteRule ^old-prices/?$ /prices/ [R=301,L]
RewriteRule ^about-the-company/?$ /about-us/ [R=301,L]

The upside: it works instantly, before PHP or WordPress start, so it is the fastest option and adds no load to the site. The downside: typos are easy, and one wrong character (an unclosed bracket, say) can take the whole site down with a 500 error.

For a handful of addresses you can write this by hand. For hundreds, use the redirects generator in Tools: paste two columns from Excel (old address, new address), and the generator:

  • turns the addresses into safe rules (special characters, accented letters, query strings),
  • catches loops, redirect chains and duplicates before they reach the server,
  • handles 410 and noindex too, for addresses that should disappear,
  • remembers earlier lists in your browser, so it is easy to go back to last week's version.

What the codes in brackets at the end of a rule mean is explained in .htaccess RewriteRule flags explained, and conditions based on variables in .htaccess variables.

2. PHP

When .htaccess is not an option (an nginx server, no permissions) or the addresses come from a database, you can redirect in PHP:

<?php
$map = [
    '/old-prices/'        => '/prices/',
    '/about-the-company/' => '/about-us/',
];
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
if (isset($map[$path])) {
    header('Location: ' . $map[$path], true, 301);
    exit;
}

I tested this on a local server: /old-prices/ answers with a 301 to /prices/. Three things that are easy to forget:

  • the third argument of header() is the response code. Without it, PHP sends a 302, a temporary redirect,
  • exit after the redirect - otherwise the script keeps running and may send the page or write to the database,
  • the query string - parse_url cuts off everything after the question mark, so /about-the-company/?utm_source=fb ends up at a clean /about-us/. If the parameters should carry over, you have to append them.

Code like this goes at the top of index.php, or into a separate file that .htaccess sends non-existent addresses to.

3. A WordPress plugin

In WordPress the simplest option is a plugin. This blog runs Redirection: you add redirects in the admin panel without touching any files, group them, and import them from a CSV file. Changed a post's address? The plugin offers a redirect from the old one by itself.

But its most valuable feature is something else: the 404 log. The plugin records every address that ended in an error, with the number of hits and the page the visitor came from. That shows you:

  • old addresses you forgot during a rebuild - which visitors still land on,
  • typos in links from other sites that are worth redirecting,
  • break-in attempts - hundreds of requests for /wp-login.php.bak, /.env or /phpmyadmin/ are bots looking for a hole. They are not dangerous in themselves, but they show what is being probed, and a sudden flood of them is a sign to check your updates and passwords.

The downside: the plugin only runs once WordPress has loaded, so each redirect costs a little more server time than an .htaccess rule. With a few hundred addresses, it makes no difference.

Which one to choose?

  • A static site or one without WordPress, a fixed list of addresses - .htaccess, ideally from the generator.
  • Addresses computed from data, an nginx server, an application in your own code - PHP (or the server configuration, if you have access to it).
  • WordPress - a plugin, if only for the 404 log. Very long, fixed lists after a migration can go into .htaccess.

The most important rule is the same for all three: after the change, check a few old addresses in a private window or with a tool like curl, and look at the 404 log for a few weeks. When .htaccess rules do not work, see Debugging .htaccess.

Advertisement