When you create a 301 or 302 redirect, you’re telling the web browser to take users who are currently on one page and send them to another page on your website. This can be helpful if you have multiple pages that are related but don’t have a clear, easy way for users to find them. ..
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:
Then, if you want to make it a permanent redirect, replace “redirect” with “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:
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:
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.
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:
To redirect HTTP to HTTPS, you’ll want to use:
This defaults to 302, but you can make it permanent by setting the return code at the end: