What are some best practices for handling subdomains and redirects in PHP?
When handling subdomains and redirects in PHP, it is important to properly set up your server configuration to recognize subdomains and redirect them accordingly. One common approach is to use a wildcard subdomain setup in your DNS settings and then handle the redirection logic in your PHP code.
// Check if the current URL has a subdomain
$subdomain = explode('.', $_SERVER['HTTP_HOST'])[0];
// Redirect based on subdomain
switch($subdomain) {
case 'subdomain1':
header('Location: https://subdomain1.yourdomain.com');
exit();
break;
case 'subdomain2':
header('Location: https://subdomain2.yourdomain.com');
exit();
break;
default:
// Handle default case or show an error page
break;
}