How to Use 301 Redirects and 302 Redirects

Short tutorial on what are redirects, when and how to usem. 301 redirect and 302 redirect examples with .htaccess, Apache / Nginx config files, PHP and more.

When you change the URL for a page you need to use a 301 redirect or a 302 redirect to send the viewer to the new page. This article will explain what are redirects, when to use them and, of course, how to create redirects with lots of examples.

What is a redirect?

URL Redirection is a way of telling browsers and search engines that a page or an entire website is not available at the old address and the user should be redirected to a new address.

When to use redirects?

There are many scenarios where you will need to use redirects:

  • You changed the URL for a page (changed its category, corrected a typo, etc.)
  • You moved your entire site to a new domain
  • You want to make sure all users go to the https version of your site instead of http
  • You deleted a page but instead of a 404, you want the viewer to see another page

Types of redirects

There are two main types of redirects:

  • 301 – Permanent
  • 302 – Temporary

There are also other codes for more sophisticated Permanent and Temporary redirects like 303, 307 and 308 but I will not get into that in this short tutorial. Chances are you will never use anything else other than 301 anyway.

There are several ways to implement the 301 redirects and 302 redirects. While they will all eventually send the viewer to the new page, this may happen faster or slower and it may also confuse the search engine in the process.

Server-side redirects (best, fast)

The best way to do it is on the server in Apache .htaccess / Nginx virtual host file.

You can also create the redirect in the server-side programming language you use (e.g. PHP, JSP) by altering the header of the response. This is very useful when you don’t have access to the .htaccess file (or Nginx config file which also needs to be reloaded).

Client-side redirects (not good for SEO, slower)

I remember the days when I was using JavaScript for this. Don’t really see any reason to do this ever again but here are the solutions:

  • meta refresh tag
  • JavaScript location property

If possible, avoid the client-side solutions (especially JavaScript).

Redirect Examples

Now that you know everything there is to know on this topic, let’s look at some redirect examples.

Redirect a page with .htaccess

Redirect 301 /oldpage/ http://www.my-domain.com/newpage/

Redirect HTTP to HTTPS with .htaccess

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://my-domain.com/$1 [L,R=301]

Redirect HTTP to HTTPS with Apache Virtual Host

<VirtualHost *:80>
...
...
   Redirect / https://my-domain.com
</VirtualHost>

Redirect the entire domain page with .htaccess

Redirect 301 / https://www.new-domain.com/

Redirect an entire category to a new category with .htaccess

RewriteEngine On
RewriteRule ^old-category(/.*|)$ /new-category/$1 [R=301,L,NC]
#or
RedirectMatch 301 /old-category/(.*) /new-category/$1

Redirect a page with Nginx

location = /oldpage {
  # 301 (permanent) redirect
  return 301 $scheme://my-domain.com/newpage

  # 302 (temporary) redirect
  # return 302 $scheme://my-domain.com/newpage
}

Redirect a page with PHP

header("Location: https://my-domain.com/new-category/newpage", true, 301);
exit();

Redirect a page with meta refresh

<!doctype html>
<html>
  <head>
  <meta http-equiv="refresh" content="0; url=https://my-domain.com/newpage" />
  <title>Example title</title>
...
</html>

Google will treat this as a permanent redirect. If you allow a number of seconds before the refresh, like content=”10…” , Google will treat is as a temporary redirect.

Redirect a page with JavaScript (Please don’t)

<script>
   window.location.href('https://www.my-domain.com/newpage')
</script>

How to create redirects in WordPress?

I always suggest using the .htaccess file when you have the possibility.

When you can’t, don’t worry, there are lots of plugins for redirecting pages in WordPress. Some SEO plugins have URL forwarding tools included (usually in the premium versions).

A good start may be a plugin with a very appropriate name for its purpose: Redirection.

Too many redirects?

If you make a mistake in your code, you might end up creating a looping (circular) redirection.

A simple scenario would be:
page1 -> page2
page2 -> page3
page3 -> page1

Usually it’s more complicated than this, like some RegEx expression including more pages than you intended, http / https mismatch, etc.

This will confuse the browser which will show something like err_too_many_redirects.

When this happens, backup your .htaccess (or config file), check all your redirects, start removing some until you resolve the issue and then gradually add them back (with any corrections you need) until you are happy with the results.

When troubleshooting your redirects remember sometimes they are not all in one place (e.g. you can have redirects in your virtual host config file, .htaccess, php, etc. and they all work together).

That’s it! Good luck keeping your visitors and search engines happy!

Leave a Reply

Your email address will not be published. Required fields are marked *