301 redirects to the homepage, while 302 redirects to the specific page you are trying to reach. 301 and 302 are both common web server requests, but they have different implications. 301 Redirects If you are trying to reach a page on a website that is not currently available, your browser will try to find the page on the server instead. This is called a 301 redirect. If the page you are trying to reach is on the website, it will be redirected to that page. If it isn’t, your browser will try to find the page on its own and will send you back to the homepage. This is called a 302 redirect. 302 Redirects If you are trying to reach a specific page on a website, your browser will first look for all pages on that website and then choose one of them as its destination. This is called a 302 redirect. If there aren’t any pages on that website, your browser will try to find the address of the server where this document was originally written and then send you back there instead of sending you back to the homepage.
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: