Remove ads
HTTP response status code From Wikipedia, the free encyclopedia
On the World Wide Web, HTTP 301 is the HTTP response status code for 301 Moved Permanently. It 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.
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
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]
A custom directory redirect, using an index.html file:
<meta http-equiv="refresh" content="0; url=/" />
<p><a href="/">Home</a></p>
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");
});
Equivalently simple for an nginx configuration:
location /old/URL {
return 301 '/new/URL';
}
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 the URL will permanently change and is not due to be changed again any time soon.[2][3]
The HTTP 301 status code has several technical nuances that developers should be aware of when implementing and managing redirections:
Seamless Wikipedia browsing. On steroids.
Every time you click a link to Wikipedia, Wiktionary or Wikiquote in your browser's search results, it will show the modern Wikiwand interface.
Wikiwand extension is a five stars, simple, with minimum permission required to keep your browsing private, safe and transparent.