How can one implement a 301 redirect from a short URL to the full URL using PHP?
When a short URL needs to be redirected to a full URL using a 301 redirect, we can achieve this by using PHP header() function. This function sends a raw HTTP header to the client, which includes the HTTP status code for a permanent redirect (301) along with the Location header specifying the full URL to redirect to.
<?php
$shortURL = "http://example.com/short";
$fullURL = "http://example.com/full";
header("HTTP/1.1 301 Moved Permanently");
header("Location: $fullURL");
exit();
?>
Keywords
Related Questions
- What potential issues could arise when trying to retrieve server information using PHP compared to Java?
- In what scenarios can the DRY (Don't Repeat Yourself) principle be applied effectively when writing PHP code?
- What are the best practices for handling form submissions in PHP to avoid security risks?