How can mod_rewrite be utilized to manage subdomains and URL redirection in a PHP application?

Mod_rewrite can be utilized in a PHP application to manage subdomains and URL redirection by creating rules in the .htaccess file. This allows for custom redirection of URLs based on specific criteria, such as subdomain or query parameters.

RewriteEngine On

# Redirect all requests to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]

# Redirect subdomains to specific pages
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]
RewriteRule ^(.*)$ subdomain.php [L]