X
Popular Searches

What’s the Difference Between 301 and 302 Redirects?

301 redirect illustration
duangphorn wiriya/Shutterstock

If you’ve moved your content around, it’s best to redirect old broken links to the working location. There are two types of redirects—302, used for testing, and 301, which is permanent, and best for SEO.

302 Is Temporary, 301 Is Permanent

Both redirect types achieve the same effect. When a user attempts to access a redirected resources (for example, the HTTP version of an HTTPS link), the web server sends a 301 or 302 response code to the user’s browser, along with a link to the intended destination. The user’s browser will then immediately make another request to the correct page, effectively redirecting them.

The difference lies in what happens the second time a user visits that page. If you’re serving a 302 redirect, the browser will see this as temporary. In the HTTPS example, the browser will continue to make requests to the insecure HTTP version of the page, and the web server will continue to send 302 response codes each time.

This is bad for your site’s performance, as a user will have to make more round trips to the web server to get to the intended destination. To solve this, you can use a 301 redirect. When the browser tries to make a second request, it will check its cache and remember that the URL is supposed to be redirected, and automatically redirect without bothering the server. A 301 redirect is supposed to be sent out once, and redirect forever.

This means a 301 redirect is very permanent. Chrome will remember one until the user clears the cache, which is a manual operation. If you accidentally made an incorrect 301 redirect, it could cause issues down the line, so you’ll always want to test changes with a 302 redirect first to verify that it works, then deploy a 301 redirect.

How Does This Affect SEO?

Most of the time, search engines like Google will see a 301 redirect and update their search rankings accordingly. If you’re changing domain names, you will want to 301 redirect your old domain’s links to the new domain’s pages. This will lead to your new domain replacing your old one in the search rankings, which is (probably) what you want.

Ultimately, having to redirect your site probably won’t hurt your SEO much. Between 90-99% of your sites ranking will transfer over when changing your entire domain name, so redirecting a few pages won’t hurt at all. Note that this only applies to 301 redirects—302 redirects are temporary, and won’t have any immediate effect on your rankings, but can drag you down in the long run if you don’t switch to 301.

How To Set Up Redirects in Nginx

In Nginx, you can implement redirects using the rewrite directive. This will match a string with a regular expression and redirect the user to a modified URL. If you simply want to redirect an old page to a new page, you can select the page name and replace it with a rewrite:

server {
  server_name www.example.com;
  rewrite ^/old_page.html$ new_page.html redirect;
}

Then, if you want to make it a permanent redirect, replace “redirect” with “permanent”:

rewrite ^/old_page.html$ new_page.html permanent;

You can use the same syntax to match multiple pages. For example, if you wanted to map an entire domain to a new domain, you could use:

server {
  server_name olddomain.com;
  rewrite ^/(.*)$ https://newdomain.com/$1 permanent;
}

For redirecting HTTP to HTTPS, you’ll instead want to use a listen block on port 80 that will redirect all traffic by manually serving a 301:

server {
  listen 80;

  server_name example.com;
  return 301 https://example.com$request_uri;
}

How to Set Up Redirects in Apache

For Apache, the setup is simple as well. For basic redirects, you can use the Redirect directive, which takes two arguments—the old page, and the new page.

Redirect /oldpage https://www.example.com/newpage

This defaults to a 302 redirect, but you can make it permanent by using “Redirect 301“.

The Redirect directive takes manual parameters, but you can use RedirectMatch to match URLs with regular expressions. For example, to remap a folder like /img to a subdomain, you can use:

RedirectMatch ^/img/(.*)$ http://media.example.com/$1

To redirect HTTP to HTTPS, you’ll want to use:

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]

This defaults to 302, but you can make it permanent by setting the return code at the end:

RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R=301,L]
Anthony Heddings Anthony Heddings
Anthony Heddings is the resident cloud engineer for LifeSavvy Media, a technical writer, programmer, and an expert at Amazon's AWS platform. He's written hundreds of articles for How-To Geek and CloudSavvy IT that have been read millions of times. Read Full Bio »

The above article may contain affiliate links, which help support CloudSavvy IT.