301 Moved Permanently
Description
The 301 Moved Permanently
status code indicates that the requested resource has been permanently moved to a new location, and any future references to this resource should use one of the returned URIs. This status code is used for permanent URL redirection.
The new permanent URI should be given by the Location
field in the response. Unless the request method was HEAD, the response should include a short hypertext note with a hyperlink to the new URI.
This status code is commonly used in the following scenarios: - When a website changes its domain name - When URL structures are reorganized - When duplicate content needs to be consolidated to a single URL - When switching from HTTP to HTTPS
Search engines and other user agents will update their references to the resource upon receiving a 301 Moved Permanently
response, which makes it the preferred method for permanent URL changes.
Syntax
The server responds with a 301 Moved Permanently
status and includes the new location:
HTTP/1.1 301 Moved Permanently
Location: https://example.com/new-path
Content-Type: text/html
Content-Length: [length in bytes]
[Optional response body with a hyperlink to the new location]
Examples
Domain Change Example
A client requests a page on an old domain that has been permanently moved to a new domain:
Client Request:
Server Response:
HTTP/1.1 301 Moved Permanently
Location: https://new-domain.com/about-us
Content-Type: text/html
Content-Length: 178
<!DOCTYPE html>
<html>
<head>
<title>Moved Permanently</title>
</head>
<body>
<h1>Moved Permanently</h1>
<p>The document has moved to <a href="https://new-domain.com/about-us">this new location</a>.</p>
</body>
</html>
HTTP to HTTPS Migration Example
A client requests a page using HTTP, but the site has migrated to HTTPS:
Client Request:
Server Response:
HTTP/1.1 301 Moved Permanently
Location: https://example.com/secure-page
Content-Type: text/html
Content-Length: 162
<!DOCTYPE html>
<html>
<head>
<title>Moved Permanently</title>
</head>
<body>
<p>This site requires HTTPS. Redirecting to <a href="https://example.com/secure-page">secure version</a>.</p>
</body>
</html>
URL Structure Change Example
A client requests a page using an old URL structure that has been permanently changed:
Client Request:
Server Response:
HTTP/1.1 301 Moved Permanently
Location: https://shop.example.com/items/365
Content-Type: text/html
Content-Length: 183
<!DOCTYPE html>
<html>
<head>
<title>Moved Permanently</title>
</head>
<body>
<p>This page has moved to our new URL structure. Redirecting to <a href="https://shop.example.com/items/365">new location</a>.</p>
</body>
</html>
Summary
The 301 Moved Permanently
status code is a critical tool for proper web resource management, ensuring that clients and search engines are directed to the current location of a resource that has been permanently relocated. It helps maintain link equity for SEO purposes, reduces user confusion by automatically redirecting to the correct resource, and ensures bookmarks and references remain functional even after URL changes. When implementing URL changes, using a 301
redirect is generally preferred over other redirection methods when the change is intended to be permanent.