Skip to content
PHP Logo

How can PHP developers ensure that outdated URLs with query parameters are properly redirected to avoid 404 errors?

When dealing with outdated URLs with query parameters, PHP developers can ensure proper redirection to avoid 404 errors by using the $_SERVER['REQUEST_URI'] variable to capture the requested URL and then checking for specific query parameters or patterns to determine the appropriate redirect location. By utilizing a conditional statement and the header() function to send a 301 redirect status code, developers can seamlessly redirect users to the correct page without encountering 404 errors.

php
if ($_SERVER['REQUEST_URI'] == '/old-url?param=value') {
    header('HTTP/1.1 301 Moved Permanently');
    header('Location: /new-url');
    exit();
}