When browsing the web, you may have encountered situations where clicking a link automatically redirects the page to another address. Or, to be more technical, as a website developer, you might need to upgrade your website’s domain http:// or https://redirect older pages to newer URLs. Behind these scenarios, an important HTTP status code is at play— 301 .
What is the 301 status code?
Basic definition
The full name of the 301 status code is ” 301 Moved Permanently ” . It is a redirection status code in the HTTP protocol, indicating that the requested resource has been permanently moved to a new location.
When a server returns a 301 status code, it is not only telling the client “this resource is no longer in its original location”, but more importantly, it is declaring “this resource will never return to its original location, please access the new address directly in the future”.
Official regulations
According to HTTP standard RFC 7231, the definition of the 301 status code is:
“The requested resource has been permanently moved to a new URI, and any future references to this resource should use one of the returned URIs.”
This means:
- Redirection is permanent .
- The old URL should be replaced with the new URL.
- Clients (such as browsers) should update the addresses where bookmarks and other data are stored.
- Search engines should transfer ranking weight to the new URLs.
How the 301 status code works
Request-Response Flow
Let’s understand the complete process of a 301 redirect through a concrete example:
Client request → Server response 301 → Client redirection → Accessing new resource
Detailed steps:
- The client sends a request
GET /old-page.html HTTP/1.1
Host: www.example.com
- Web server returned 301 response
HTTP/1.1 301 Moved Permanently
Location: https://www.example.com/new-page.html
Content-Type: text/html
Content-Length: 154
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="https://www.example.com/new-page.html">here</a>.</p>
</body>
</html>
- Client automatic redirection
- After the browser receives a 301 response
- Automatically read the new address from the Location header
- Send a request to the new address
- Completed resource access
GET /new-page.html HTTP/1.1
Host: www.example.com
Technical details
Key response header:
Location: new-url– Specify a new resource address (required)Content-Type: text/html– Response body typeContent-Length: xxx– Response volume length
Browser behavior:
- Automatic redirection is usually imperceptible to users.
- Update the address bar to display the new URL
- Bookmarks may be updated (modern browsers typically do not automatically update bookmarks created manually by the user).
Practical application scenarios of 301 redirects
1. Website domain name change
When a company changes its brand or unified domain name, it needs to permanently redirect the old domain name to the new domain name.
Example: Redirect http://old-company.com to https://new-company.com
# Apache server configuration (.htaccess)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old-company\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.old-company\.com$
RewriteRule ^(.*)$ https://new-company.com/$1 [R=301,L]
2. Upgrade from HTTP to HTTPS
After enabling an SSL certificate on a website, all HTTP requests need to be redirected to HTTPS.
# Nginx server configuration
server {
listen 80;
server_name example.com www.example.com;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl;
server_name example.com;
# SSL configuration and normal processing logic
}
3. URL structure optimization
Improve website URL structure to enhance user experience and SEO performance.
Old URL: https://example.com/products.php?id=123&category=books
New URL: https://example.com/books/123/
# Redirect a dynamic URL to a static URL
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=([0-9]+)&category=([a-z]+)$
RewriteRule ^products\.php$ /%2/%1/? [R=301,L]
4. Standardize and unify URLs
Ensure that your website has only one valid URL version to avoid duplicate content.
Common standardization scenarios:
- With www vs without www
- Slash at the end vs. no slash at the end
- URLs with inconsistent capitalization
# Use the version with www in the name.
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
# Add a trailing slash to all characters
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ https://www.example.com/$1/ [R=301,L]
5. Permanently delete or merge pages
When the page content is merged into another page or permanently deleted.
Example: Redirect discontinued-product.html to similar-products.html
<!-- If the server cannot be configured, HTML meta refresh can be used (not recommended as the first choice) -->
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="refresh" content="0; url=https://example.com/similar-products.html">
</head>
<body>
This product is no longer available. Redirecting you to a page of similar products...
</body>
</html>
Difference between 301 and other redirection status codes
HTTP redirection status code family
| status codes | name | characteristic | caching behavior | Application scenarios |
|---|---|---|---|---|
| 301 | Moved Permanently | Permanent redirect | Cacheable | Domain name change, HTTPS upgrade |
| 302 | Found | Temporary redirection | Normally not cached | Temporary maintenance, A/B testing |
| 307 | Temporary Redirect | Temporary redirection, keep the method | Normally not cached | Redirection of request method needs to be preserved |
| 308 | Permanent Redirect | Permanent redirection, keep method | Cacheable | Permanent redirection that needs to retain the request method |
301 vs 302: Key Differences
301 (Permanent Redirect):
- Search engines will pass weight to new URLs.
- Browsers and proxy servers can cache redirects.
- Subsequent requests should directly access the new address.
302 (Temporary Redirect):
- Search engines maintain the weight of old URLs.
- Normally not cached
- Subsequent requests should continue to access the original address.
Recommendation:
- If the redirect is permanent , use a 301 redirect.
- If the redirect is temporary , use a 302 redirect.
301 vs 308: Methodology Retention
Limitations of 301:
In some cases, a 301 redirect may convert a POST request into a GET request, which could result in data loss.
Advantages of 308:
The 308 status code ensures that the request method remains unchanged during redirection.
# 301 May Change Request Method POST /old-location HTTP/1.1 ↓ HTTP/1.1 301 Moved Permanently Location: /new-location ↓ GET /new-location HTTP/1.1 # The method has been changed from POST to GET # 308 Keep Request Method POST /old-location HTTP/1.1 ↓ HTTP/1.1 308 Permanent Redirect Location: /new-location ↓ POST /new-location HTTP/1.1 # Method maintains POST
301 redirect configuration method
1. Apache server configuration
.htaccess file configuration:
# Enable rewrite engine
RewriteEngine On
# Domain redirection
RewriteCond %{HTTP_HOST} ^old-domain\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.old-domain\.com$ [NC]
RewriteRule ^(.*)$ https://new-domain.com/$1 [R=301,L]
# HTTP to HTTPS redirection
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Specific page redirection
Redirect 301 /old-page.html https://example.com/new-page.html
2. Nginx server configuration
nginx.conf or site configuration file:
server {
listen 80;
server_name old-domain.com www.old-domain.com;
return 301 https://new-domain.com$request_uri;
}
server {
listen 80;
server_name example.com;
return 301 https://$server_name$request_uri;
}
# Specific path redirection
location /old-path/ {
return 301 https://example.com/new-path/;
}
3. PHP Application Configuration
<?php
// Simple 301 redirect
header("HTTP/1.1 301 Moved Permanently");
header("Location: https://example.com/new-page.html");
exit();
// 301 redirect with logging
function permanentRedirect($newUrl) {
// Record redirection information (for analysis)
error_log("301 Redirect: " . $_SERVER['REQUEST_URI'] . " -> " . $newUrl);
// Set response headers
header("HTTP/1.1 301 Moved Permanently");
header("Location: " . $newUrl);
exit();
}
// Usage Example
if ($_SERVER['REQUEST_URI'] == '/old-page') {
permanentRedirect('/new-page');
}
?>
4. Node.js/Express Configuration
const express = require('express');
const app = express();
// Simple 301 redirect
app.get('/old-page', (req, res) => {
res.status(301).redirect('https://example.com/new-page');
});
// Batch redirection configuration
const redirects = {
'/old-path1': '/new-path1',
'/old-path2': '/new-path2',
'/blog/2020/*': '/news/*'
};
// Apply redirection rules
Object.keys(redirects).forEach(oldPath => {
app.get(oldPath, (req, res) => {
let newPath = redirects[oldPath];
// Handling wildcards
if (oldPath.endsWith('*')) {
const wildcardMatch = req.path.replace(oldPath.slice(0, -1), '');
newPath = newPath.replace('*', wildcardMatch);
}
res.status(301).redirect(newPath);
});
});
SEO impact of 301 redirects
Weight transfer (Link Juice)
The most important SEO feature of a 301 redirect is its ability to transfer page authority from the old URL to the new URL.
The content to be transmitted includes:
- PageRank value
- Anchor text weight
- Ranking Signals
- Authority
Weight transfer formula (approximate):
The weight gained by the new URL ≈ the original weight of the old URL × the transitivity factor
This transfer factor is typically considered to be 85-99% , not 100%, so there may be a slight weight loss during the redirection process.
Best SEO Practices
- Avoid redirection chains : Unrecommended: A → B → C → D Recommended: A → D
- Ensure crawlability
- Avoid circular redirection
- Ensure the redirection target returns a 200 status code.
- Do not redirect to irrelevant content.
- Update internal links
- Update your website’s internal links to the new URLs as soon as possible.
- Reduce reliance on redirection
- Monitor index status
- Monitor the indexing status of new and old URLs in Google Search Console
- Ensure that old URLs are gradually replaced by new URLs.
Common problems and troubleshooting
1. Redirecting loops
Symptom: The browser displays an “Too many redirects” error.
Common causes:
- A redirects to B, and B then redirects back to A.
- HTTPS redirection configuration error
Solution:
# Check Apache configuration to avoid loops
RewriteCond %{HTTPS} off
# Add conditions to prevent already redirected requests from being redirected again
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
2. Weight transfer failed.
Symptom: The new URL is not achieving the expected search ranking.
Possible reasons:
- Redirect chain too long
- The target URL itself has technical issues.
- The search engine has not yet re-crawled and indexed it.
Solution:
- Use SEO tools to check weight transfer.
- Submit a new sitemap in Search Console
- Ensure the target URL returns a 200 status code.
3. Performance issues
Symptom: Website loading speed slows down
Reason: Excessive redirects increase the number of additional HTTP requests.
Optimization suggestions:
# Use 301 instead of 302 to leverage browser caching. # Merge multiple redirects into one
Test and verify 301 redirects
1. Online tool testing
Recommended tools:
- Redirect Checker (https://redirect-checker.org)
- HTTP Status Code Checker
- Google Search Console URL Inspection Tool
2. Command line testing
# Use curl to test redirection curl -I http://example.com/old-page # Output example: # HTTP/1.1 301 Moved Permanently # Location: https://example.com/new-page # Follow the redirect and display details curl -L -v http://example.com/old-page
3. Browser developer tools
In the developer tools of Chrome or Firefox:
- Open the “Network” tab
- Access the redirected URL
- Check if the Status Code of the first request is 301.
- Check the Location field in the Response Headers
Summarize
The 301 status code, as the most important redirection type in the HTTP protocol, plays a crucial role in website maintenance, architecture optimization, and SEO strategies. It’s more than just a technical status code; it’s a bridge connecting a website’s past and future.
Key takeaways:
- 301 indicates a permanent redirect.
- Able to transfer SEO weight to new URLs
- Should be used for permanent URL changes
- When configuring, be careful to avoid redirection loops.
- Regularly monitor and test the redirection effect.
Throughout a website’s lifecycle, the proper use of 301 redirects ensures a seamless user experience, maintains search engine rankings, and provides technical support for the website’s continued development. Whether it’s a simple page migration or a complex website restructuring, mastering the principles and applications of 301 redirects is an essential skill for modern web developers and SEO experts.