HTTP 301

From Wikipedia, the free encyclopedia
  (Redirected from 301 redirect)
Jump to navigation Jump to search

The HTTP response status code 301 Moved Permanently is used for permanent redirecting, meaning that links or records returning this response 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.

RFC 2616[1] states that:

  • If a client has link-editing capabilities, it should update all references to the Request URL.
  • The response is cacheable unless indicated otherwise.
  • Unless the request method was HEAD, the entity should contain a small hypertext note with a hyperlink to the new URL(s).
  • If the 301 status code is received in response to a request of any type other than GET or HEAD, the client must ask the user before redirecting.

Examples[edit]

Client request:

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

Server response:

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

Using an .htaccess file[edit]

To fix problems with non-existing files or directories using a distributed .htaccess file:

Redirect 301 /calendar.html /calendar/
Redirect 301 /not_found.html /

Here is an example using a .htaccess file to redirect a non-secure URL to a secure address without the leading "www":

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]

Static HTML[edit]

A custom directory redirect, using an index.html file:

<meta http-equiv="refresh" content="0; url=/" />
<p><a href="/">Home</a></p>

Using programming languages[edit]

Here is an example using Perl CGI.pm:

print redirect("https://example.com/newpage.html");

Here is an example using a PHP redirect:

<?php
header("Location: https://example.com/newpage.html", true, 301);
exit;

Here is one way to redirect using Express.js:

app.all("/old/url", (req, res) => {
    res.redirect(301, "/new/url");
});

Caching server[edit]

Equivalently simple for an nginx configuration:

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

Search engines[edit]

Both Bing and Google recommend using a 301 redirect to change the URL of a page as it is shown in search engine results, providing that URL will permanently change and is not due to be changed again any time soon.[2][3]

See also[edit]

References[edit]

  1. ^ Fielding, et al (1999-06). "10.3.2 301 Moved Permanently". RFC 2616, p 61. IETF, June 1999. Retrieved from https://tools.ietf.org/html/rfc2616#section-10.3.2.
  2. ^ Site Move Tool - Bing Webmaster Help & How-to - https://www.bing.com/webmaster/help/how-to-use-the-site-move-tool-bb8f5112
  3. ^ 301 redirects - Google Webmaster Tools Help - https://support.google.com/webmasters/bin/answer.py?hl=en&answer=93633

Bibliography[edit]

301 HTTPS