How can mod_rewrite be utilized to redirect users to different pages based on their domain in PHP?

To redirect users to different pages based on their domain in PHP using mod_rewrite, you can first set up rewrite rules in your .htaccess file to redirect requests to a PHP script. In the PHP script, you can check the HTTP_HOST variable to determine the domain and then redirect the user accordingly.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain1.com [NC]
RewriteRule ^(.*)$ redirect.php [L]

RewriteCond %{HTTP_HOST} ^domain2.com [NC]
RewriteRule ^(.*)$ redirect.php [L]
```

In the `redirect.php` script:

```php
<?php
$domain = $_SERVER['HTTP_HOST'];

if($domain == 'domain1.com') {
    header('Location: http://domain1.com/newpage');
    exit;
} elseif($domain == 'domain2.com') {
    header('Location: http://domain2.com/anotherpage');
    exit;
} else {
    header('Location: http://defaultdomain.com');
    exit;
}
?>