HTTP 301

The HTTP response status code 301 Moved Permanently is used for permanent URL redirection, meaning current links or records using the URL that the response is received for should be updated. The new URL should be provided in the Location field included with the response. The 301 redirect is considered a best practice for upgrading users from HTTP to HTTPS.[1] RFC 2616 states that:

Example

Client request:

GET /index.php HTTP/1.1
Host: www.example.org

Server response:

HTTP/1.1 301 Moved Permanently
Location: http://www.example.org/index.asp

Here is an example using a htaccess file to redirect to a non www with a SSL attached to the domain.

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

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

Here is an example using a PHP redirect.

<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://example.com/newpage.html");
exit();
?>

Equivalently simple for an nginx configuration.

location /old/url/ {
    return 301 /new/url;
}

Search engines

Both Bing and Google recommend using a 301 redirect to change the URL of a page as it is shown in search engine results.[3][4]

See also

References

  1. "Secure your site with HTTPS". support.google.com. Google. Retrieved 6 February 2016.
  2. How long do browsers cache HTTP 301s? - http://stackoverflow.com/questions/9130422/how-long-do-browsers-cache-http-301s
  3. Site Move Tool - Bing Webmaster Help & How-to - https://www.bing.com/webmaster/help/how-to-use-the-site-move-tool-bb8f5112
  4. 301 redirects - Google Webmaster Tools Help - http://support.google.com/webmasters/bin/answer.py?hl=en&answer=93633


This article is issued from Wikipedia - version of the Wednesday, March 02, 2016. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.