How do I rewrite URLs based on HTTP_HOST using .htaccess?

You can rewrite URLs based on the `HTTP_HOST` header using `.htaccess` and the `RewriteCond` and `RewriteRule` directives. Here’s how you can achieve this:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

In this example:

– `RewriteEngine On` enables the rewriting engine.
– `RewriteCond %{HTTP_HOST} ^example\.com$ [NC]` checks if the `HTTP_HOST` header matches `example.com` (case insensitive).
– `RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]` redirects all requests to `http://www.example.com/`, appending the requested URI.

Here’s what each part of the `RewriteRule` means:

– `^(.*)$`: Matches any URL path.
– `http://www.example.com/$1`: Redirects to `http://www.example.com/`, appending the requested URI captured by `(.*)`.
– `[R=301,L]`: Specifies that it’s a permanent redirect (`R=301`) and that this is the last rule to be applied (`L`).

You can adjust the `RewriteCond` and `RewriteRule` to match your specific requirements. For example, if you want to rewrite URLs for multiple domains to a single domain, you can add additional `RewriteCond` lines for each domain and adjust the `RewriteRule` accordingly.

Remember to replace `example.com` with your actual domain name.

After adding these directives to your `.htaccess` file, save the changes, and then test to ensure that the URL rewriting works as expected.