Redirecting From One Domain To Another Using Mod Rewrite

6th January 2012

Use the following rules in your Apache configuration (or your .htaccess file) to redirect all traffic from one domain to another. This also keeps the query string in place so that the user isn't just dumped to the homepage.

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

These rules are mod_rewrite dependent, so you'll need that module enabled in your Apache install.

Add new comment

The content of this field is kept private and will not be shown publicly.