/ HTTP Status Codes

301 Moved Permanently

The 301 Moved Permanently status code is an HTTP response status code that indicates the requested resource has been permanently moved to a new location, and that future requests should be directed to the new URL. This status code is typically used when a website’s content has been relocated, or when a URL structure has been updated for SEO or other purposes.

This status code is beneficial for both users and search engines. It helps users by seamlessly redirecting them to the new location of the resource, and it helps search engines by informing them that the resource’s URL has changed, allowing them to update their index accordingly.

Use Cases for 301 Moved Permanently

Some common use cases for the 301 Moved Permanently status code include:

  1. Website migration: When migrating a website to a new domain, it’s essential to use 301 Moved Permanently to redirect users and search engines to the new domain, preserving search engine rankings and ensuring a smooth transition.
  2. URL structure changes: If a website’s URL structure is updated, perhaps for SEO reasons or to improve user experience, the 301 Moved Permanently status code can be used to redirect users from the old URLs to the new ones.
  3. Consolidating duplicate content: If multiple URLs contain the same content, using a 301 Moved Permanently redirect can help consolidate these duplicate pages into a single URL, improving SEO and user experience.

Example of 301 Moved Permanently in Action

To better understand how the 301 Moved Permanently status code works, let’s examine a sample HTTP request and response.

HTTP Request

Suppose you have recently restructured your website and updated the URL for a blog post. A user tries to access the old URL:

GET /old-blog-post-url HTTP/1.1
Host: example.com

HTTP Response

Upon receiving the request, your server recognizes that the resource has been permanently moved to a new URL and sends the following response:

HTTP/1.1 301 Moved Permanently
Location: https://example.com/new-blog-post-url

In this response, the 301 Moved Permanently status code informs the client that the requested resource has been permanently moved, and the Location header provides the new URL. The client’s web browser will then automatically redirect the user to the new URL.

Implementing 301 Moved Permanently Redirects

To implement a 301 Moved Permanently redirect, you can use server-side configurations or server-side scripting languages. Here are some examples using popular web servers and programming languages:

Apache

In the .htaccess file, you can use the Redirect directive:

Redirect 301 /old-blog-post-url /new-blog-post-url

Nginx

In the Nginx configuration file, you can use the rewrite directive:

server {
    ...
    rewrite ^/old-blog-post-url$ /new-blog-post-url permanent;
    ...
}

PHP

You can use the header function in a PHP script:

header('Location: https://example.com/new-blog-post-url', true, 301);
exit;

Node.js (Express)

In a Node.js application using the Express framework, you can use the redirect method:

app.get('/old-blog-post-url', (req, res) => {
  res.redirect(301, '/new-blog-post-url');
});

Summary

The 301 Moved Permanently status code is an essential tool for managing URL redirects, ensuring a seamless user experience and maintaining search engine rankings when a resource’s URL changes. By understanding its use cases and how to implement it in various web server environments, you can effectively handle website migrations, URL structure changes, and duplicate content consolidation.

Was this helpful?

Thanks for your feedback!