What are the recommended HTTP status codes to use when implementing URL redirects in PHP?
When implementing URL redirects in PHP, it is recommended to use HTTP status codes to indicate the type of redirect being performed. The most commonly used status codes for redirects are 301 (Moved Permanently) and 302 (Found). A 301 redirect is used when a URL has permanently moved to a new location, while a 302 redirect is used for temporary redirects.
// Redirect to a new URL using a 301 status code (Moved Permanently)
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.newurl.com");
exit;
// Redirect to a new URL using a 302 status code (Found)
header("HTTP/1.1 302 Found");
header("Location: http://www.newurl.com");
exit;
Related Questions
- Are there best practices for handling form validation and submission in PHP to ensure all required fields are filled out?
- What are the common pitfalls or misconceptions that PHP learners should be aware of when following code examples from external sources, and how can they avoid falling into these traps?
- In PHP, what are some common security considerations when saving and retrieving user input data for future visits to a website?