301 Moved Permanently and 302 Found are HTTP status codes sent in response to web clients and search engine bots when trying to access a redirected URL.
The HTTP status code is the first line in HTTP headers used in a web server response. 301 and 302 codes are described in details in the HTTP specification.
301 Moved Permanently
A 301 status means that the page has been permanently moved to another URL. Web clients should not access the old URL anymore and from now on they will be using the new one.
When this status is received, search engines (Google, Bing) will replace the old URL with the new one in their database. From then on, the search engine results will display only the new URL.
When there is a 301 redirect from sourcedomain.com to targetdomain.com, the web client and the server will exchange the following HTTP headers:
The browser requests loading sourcedomain.com with the sample HTTP request headers:
Request-Line | GET / HTTP/1.1 |
HTTP headers (request) | Host: sourcedomain.com |
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) Firefox/46.0 | |
Accept: text/html,application/xhtml+xml | |
The server will respond to the request with these HTTP headers:
Status Line | HTTP/1.1 301 Moved Permanently |
HTTP headers (response) | Date: Fri, 16 May 2016 12:20:50 GMT |
Server: Apache | |
Location: http://targetdomain.com/ | |
The browser, in this case Mozilla Firefox, will read this response and send a new request to the URL in the Location field. It will also replace sourcedomain.com in the bookmarks with targetdomain.com.
When to Use 301 Redirect?
– When the website’s domain is changed; for example, when mysitename.com/blog from now on will be blog.mysitename.com or somesitename.com will be mysitename.bg;
– When a certain website is accessed from a few different URLs, such as: http://home.mysitename.com, http://www.mysitename.com, http://mysitename.net (parked domains), etc.;
– When the website will be using a new structure and new URLs.
302 Found
A 302 status means that the page is temporarily located on a different URL. Web clients must keep using the original URL.
When to Use 302 Redirect?
– Only when the redirect is really temporary;
– For example, when short maintenance is performed or access to the web source is temporarily restricted;
– In cases when a webpage is not ready to be published yet, but you would like to display something instead of it for a while (a coming soon page or something else).
Setting Up a 301 or 302 Redirect
Under the (Domain) Redirects menu in cPanel you can setup automatic redirects for domains and URLs. You can also use the Aliases menu in cPanel to redirect an alias.
Redirect might be implemented by setting up rules in the .htaccess file.
If you setup a redirect through .htaccess, you might need to clear the browser cache, in order to see the results.
HTTP status codes are part of the HTTP headers and can be managed by the applications in the hosting account. For example, setting up a 301 redirect by using a PHP code:
<?php header("HTTP/1.1 301 Moved Permanently"); header("Location: http://targetdomain.com/new.html"); exit(); ?>