What is the difference between 301 and 302 redirects? – CloudSavvy IT

301 redirection illustration
duangphorn wiriya / Shutterstock

If you have moved your content around, it is best to redirect old broken links to the workplace. There are two types of redirects – 302, which is used for testing, and 301, which is permanent and best for SEO.

302 is temporary, 301 is permanent

Both redirection types achieve the same effect. When a user attempts to access a redirected resource (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 a new request to the correct page, effectively redirecting them.

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

This is bad for the performance of your site, as a user will have to make several trips to the web server to get to the intended destination. To resolve this you can use a 301 redirect. When the browser tries to make another 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 redirected forever.

This means that a 301 redirect is very permanent. Chrome remembers one until the user clears the cache, which is a manual action. If you accidentally did the wrong 301 redirect, it could cause problems later on, so you’ll always want to test changes with a 302 redirect first to confirm that it works and then implement 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 change domain names, you will want to 301 redirect your old domain’s links to the new domain’s pages. This will cause your new domain to replace your old one in the search rankings, which (probably) is what you want.

In the end, it will probably not hurt your SEO much to have to redirect your site. Between 90-99% of your site’s ranking will be transferred when you change your entire domain name, so redirecting a few pages will not hurt at all. Note that this only applies to 301 redirects – 302 redirects are temporary and will have no immediate effect on your rankings, but can drag you down in the long run if you do not switch to 301.

How to set up redirects in Nginx

In Nginx, you can implement redirects using rewrite directive. This will match a string with a regular expression and redirect the user to a changed URL. If you just 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;

So, 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 want to link an entire domain to a new domain, you can use:

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

To redirect HTTP to HTTPS, you will instead use a listening block on port 80 that redirects all traffic by manually operating 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, setup is also simple. For basic redirects you can use Redirect Directive, which takes two arguments – the old side and the new side.

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

This is a 302 redirect by default, but you can do it permanently 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 as /img for a subdomain, you can use:

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

To redirect HTTP to HTTPS you will need:

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

This is the default for 302, but you can make it permanent by setting the return code at the end:

RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R=301,L]

Leave a Comment