301 Moved Permanently
Fault: Neither
TL;DR
All future requests should use the new URL provided in the Location header.
The resource has been permanently moved to a new URL.
What This Error Means
The resource has been permanently moved to a new URL.
All future requests should use the new URL provided in the Location header.
Common Causes
- Website migration
- URL structure change
- Permanent content relocation
How to Fix It (For Visitors)
- Update bookmarks to new URL
- Browser should automatically redirect
How to Fix It (For Developers/Admins)
- Update internal links to new URL
- Use for permanent URL changes
- Set Location header
Code Examples
Here's how to return a 301 status code in various programming languages:
Python (Flask)
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/endpoint')
def endpoint():
return jsonify({"message": "Moved Permanently"}), 301
Node.js (Express)
const express = require('express');
const app = express();
app.get('/endpoint', (req, res) => {
res.status(301).json({ message: 'Moved Permanently' });
});
PHP
<?php
http_response_code(301);
header('Content-Type: application/json');
echo json_encode(['message' => 'Moved Permanently']);
?>
Go
package main
import (
"encoding/json"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(301)
json.NewEncoder(w).Encode(map[string]string{
"message": "Moved Permanently",
})
}
Java (Spring Boot)
@GetMapping("/endpoint")
public ResponseEntity<Map<String, String>> endpoint() {
Map<String, String> response = new HashMap<>();
response.put("message", "Moved Permanently");
return ResponseEntity.status(301).body(response);
}
Ruby (Sinatra)
get '/endpoint' do
status 301
json message: 'Moved Permanently'
end
Browser Compatibility
| Browser | Support | Notes |
|---|---|---|
| Chrome | ✓ Full Support | All versions |
| Firefox | ✓ Full Support | All versions |
| Safari | ✓ Full Support | All versions |
| Edge | ✓ Full Support | All versions |
Official Specification
This status code is defined in RFC 7231 Section 6.4.2.