Fork me on GitHub

Note! In most cases redirects are better done in WordPress/PHP e.g. the Redirection plugin, using the https://codex.wordpress.org/Rewrite_API or with custom code in e.g. theme functions.php. Use Nginx redirects only if there is no WordPress way to do it, for example when diverting traffic from the index.php to another custom PHP script on the same site.

Basic redirects in WordPress/PHP

In PHP code you can express whatever redirection logic you want without being confined to the limited features on Nginx. For example there could be a file mu-plugins/redirects.php that would include:

// Redirect any requests for www.esimerkki.fi or esimerkki.fi to example.com/fi/
if ( isset($_SERVER['HTTP_HOST']) && strpos($_SERVER['HTTP_HOST'], 'esimerkki.fi') !== false ) {
  header("Location: https://example.com/fi/", true, 301);
  die();
}

Another more elaborate one example would be:

switch ($_SERVER['HTTP_HOST']) {

  # Enforce no www
  # Use 301 to make redirect permanent and cached
  # Use 302 for temporary (non-cached) redirects
  case "www.example.com":
    header("Location: https://example.com/", true, 301);
    break;

  # Multiple extra domains to same canonical domain
  # Note! Many plugins already do this automatically, e.g. Seravo Plugin or Polylang
  case "example.org":
  case "exmple.net":
  case "example.info":
    header("Location: https://example.com/", true, 301);
    break;

  # Localized domain to subfolder
  case "example.fi":
    header("Location: https://example.com/fi/", true, 301);
    break;

  # Localized domain to subfolder
  case "example.de":
    header("Location: https://example.com/de/", true, 301);
    break;

  default:
    header("Location: https://www.happy-or-not.com/en/", true, 301);

}

Basic redirects in Nginx

Redirects from http://your-site.com/original -> https://example.com/new/url/

rewrite ^/original(.*)$ https://example.com/new/url$1 permanent;

Rewrite all old *.html files to WordPress pages with pretty URLs:

rewrite ^/([0-9a-z-]+)\.html /$1/ permanent;

Serve country pages (example.es, example.de etc) from custom PHP file

if ($host ~ "example.es|example.de") {
  rewrite ^(/)?$ /country-pages/index.php last;
}

Please use something like Rexexpal to test your regular expressions and be sure to use curl -IL to test your redirects without having to hassle with the cache of a regular browser.

Redirecting domains in Nginx

If you have multiple domains for your site and want to only use one of them:

if ($host ~ "old-subdomain.your-old-site.com") {
  return 301 https://your-site.com$request_uri;
}

Testing redirects

Please use curl to test redirects. Using a browser for testing will not work as the browser in most cases will cache the first redirect and after that no changes will be visible when testing with a broser. Using curl ensures there is no caching at all and it will print location: headers that show clearly what the redirect is.

Example:

$ curl -IL example.org
HTTP/1.1 200 OK
X-Cache: HIT
Location: https://example.com/
Content-Length: 100

HTTP/1.1 200 OK
X-Cache: HIT
Content-Length: 1270