Can I set up URL rewriting based on HTTP_HOST using .htaccess?

Yes, you can set up URL rewriting based on the `HTTP_HOST` header using `.htaccess`. This allows you to redirect or rewrite URLs based on the domain name in the HTTP request. Here’s how you can achieve this:

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

In this example:

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

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

– `^(.*)$`: Matches any URL path.
– `http://newdomain.com/$1`: Redirects to `http://newdomain.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 different destinations, you can add additional `RewriteCond` lines for each domain and adjust the `RewriteRule` accordingly.

Remember to replace `oldomain.com` and `newdomain.com` with your actual domain names.

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