SEO – Redirection

A redirect sends the visitor (and the search engine bot) from one URL to another URL automatically. When you move a page, change the URL, or delete a page, a redirect makes sure that people and the ranking signals of the old page reach the new one.

When do we need a redirect?

  • You changed the URL of a page or the structure of the site.
  • You moved to a new domain.
  • You moved from http to https.
  • You want only one version of the site, with or without www.
  • You deleted a page and there is a closely related page to send the visitors.

Types of redirects

CodeNameMeaningUse it for
301Moved permanentlyThe page moved for everChanged URLs, domain move, http to https
302Found (temporary)The move is temporaryA short test or a temporary offer page
307Temporary redirectTemporary, and keeps the request methodTemporary redirect for forms and APIs
308Permanent redirectPermanent, and keeps the request methodPermanent redirect for forms and APIs

For normal pages, a 301 is the one you need most of the time. Google treats a 301 as a strong signal that the new URL is the one to keep, and ranking signals move to it.

Redirect with .htaccess (Apache)

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

# http to https for the whole site
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# non-www to www
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

Redirect with Nginx

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

Redirect with PHP

<?php
header("Location: https://example.com/new-page/", true, 301);
exit;

Call header() before any output is sent, and always write exit after it.

Redirect in WordPress

You can use a redirect plugin (for example Redirection, or the redirect manager of Yoast SEO Premium or Rank Math). Type the old URL and the new URL, and choose 301. The plugin keeps a list, so it is easy to manage.

Mistakes to avoid

  • Redirect chains: A goes to B, then B goes to C. Each step is slow. Redirect A directly to C.
  • Redirect loops: A goes to B and B goes back to A. The page never opens.
  • Redirecting everything to the home page. If the new page is not related, Google may treat it as a soft 404. Redirect to the closest matching page, or let it be a real 404 or 410.
  • Using JavaScript or meta refresh redirects when a server redirect is possible. Server side redirects are clearer for search engines.
  • Forgetting to update internal links. Link directly to the new URL.

Check a redirect

curl -I http://example.com/old-page/

The result should show HTTP/1.1 301 Moved Permanently and a Location: line with the new address. You can also use online redirect checker tools.

Note: Keep the redirects of an old URL for at least one year. For a domain move, keep them as long as you can. Read the .htaccess chapters on this site for more Apache rules. Next: Robots.txt.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
Redirect old URLs to new URLs with .htaccess
Interview Questions & Answers
What is the difference between a 301 and a 302 redirect?
What is a 404 error and a soft 404?
How do you do a website migration without losing SEO?