Use Mod Rewrite to redirect visitors requesting a specific host to a new host

To redirect visitors requesting a specific host name to a whole new host name, use the following snippet in your .htaccess file:

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{HTTP_HOST} ^(www\.)?oldhost\.com$
  RewriteRule ^(.*)$ http://www.newhost.com/$1 [R,L]
</IfModule>

This will check for visitors requesting either oldhost.com or www.oldhost.com and redirect them to www.newhost.com.

Comments

I find MOD_REWRITE to be:

1. A complete pain in the keister to code and
2. A godsend when you _do_ get it right.

If you insert [R=301,L] the redirect will be a "Moved Permanently" which will make it all official and stuff.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <i> <b> <cite> <code> <a>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
CAPTCHA
Are you human?