How a 301 works
When a browser asks for the old URL, the server answers with the status code 301 Moved Permanently and a Location header naming the new URL, and the browser requests that one instead. Google's documentation says Googlebot follows the redirect and treats the new URL as the one to show in search results.
Do you keep your rankings?
Google treats a permanent redirect as a signal that the new URL should replace the old one. For a site move, it says to keep redirects for as long as possible, generally at least a year, so it can pass all signals to the new URLs, including links from other sites.
Rankings are usually lost at the target, not the redirect. Google warns that sending many old URLs to one unrelated page, such as the home page, can be treated as a soft 404. Send each old URL to the page that replaces it, and in one hop: Googlebot follows up to 10 redirects in a chain, but Google advises going straight to the final URL.
301, 302 and the other redirects
Google's documentation sorts them by how long the move lasts:
- 301 and 308: permanent. Google shows the new URL in results.
- 302, 303 and 307: temporary. Google keeps showing the old URL, for example while a service is briefly unavailable.
- Meta refresh: a redirect set in the page's HTML. Google treats an instant one as permanent and a delayed one as temporary.
- JavaScript redirect: use it only when a server redirect or meta refresh is not possible, Google says.