jonathanbj_A_guy_at_a_computer_creating_wildcard_redirects-php
Home > Internet marketing and seo blog > Technical > Create wildcard redirects in php

Create wildcard redirects in php

In PHP, you can create wildcard redirects by using regular expressions to match patterns in the requested URLs and then redirecting based on those patterns. You can achieve this using the preg_match() function to match the URL against a regular expression pattern and then use header("Location: ...") to perform the redirect.

Here’s an example of how you can create a wildcard redirect in PHP using regular expressions:

// Get the requested URL
$requestUri = $_SERVER['REQUEST_URI'];

// Define regular expression pattern for matching specific patterns
$pattern = '/^\/user\/(\d+)\/?$/'; // This pattern matches URLs like /user/{user_id}/

// Check if the requested URL matches the pattern
if (preg_match($pattern, $requestUri, $matches)) {
    // Extract the user ID from the URL
    $userId = $matches[1];
    
    // Redirect to a specific user profile page
    header("Location: /profile.php?user_id=$userId");
    exit();
} else {
    // Handle other cases or show an error page
    echo "Page not found";
}

In this example, the regular expression pattern '/^\/user\/(\d+)\/?$/' matches URLs like /user/{user_id}/. If the requested URL matches this pattern, it extracts the user ID and redirects to a specific user profile page. You can modify the regular expression pattern to match different URL patterns according to your requirements.

Redirect all subdomains (www and non-www for example)

To redirect all the specified versions of subdomains to the same destination, you can use the following code:

// Get the requested URL
$requestUri = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

// Define regular expression pattern for matching different subdomains and the root path
$pattern = '/^(www\.)?example\.com\/?$/i';

// Check if the requested URL matches the pattern
if (preg_match($pattern, $requestUri)) {
    // Redirect to the desired destination (in this case, the root of the website)
    header("Location: https://example.com/");
    exit();
} else {
    // Handle other cases or show an error page
    echo "Page not found";
}

In this code, the regular expression '/^(www\.)?example\.com\/?$/i' matches URLs like http://example.com/, https://example.com/, http://wwwexample.com/, and https://wwwexample.com/ case-insensitively. If the requested URL matches this pattern, it redirects to https://example.com/. If the URL doesn’t match this pattern, you can handle other cases or show an error message as needed.

Please note that you might need to adjust the regular expression pattern according to your specific requirements or domain variations. The i modifier in the regular expression pattern makes the matching case insensitive, allowing it to match variations in capitalization.

Please note that handling redirects in this way can be powerful, but it also requires careful consideration of the regular expressions used to avoid unintended matches and redirection loops.