.htaccess Forward to New Domain with URL Parameters

If you move your web page to a new domain, you may want to forward users (and search engines in particular) that still access your page via the old domain to the new domain. However, if you use an underlying CMS or blog system you will want to keep the parameters after the domain name. E.g., if someone called http://olddomain.com/lang/en-us/living-with-complexity you would want to forward it to http://newddomain.com/lang/en-us/living-with-complexity. For this purpose, I found the following .htaccess snippet handy:

Options +FollowSymlinks
RewriteEngine On

#if domain is olddomain.com...
RewriteCond %{HTTP_HOST} olddomain.com$ [NC] #NC means non case sensitive

#...forward to newdomain.com and preserve parameters
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,NC,L] #R=301 redirects and sends HTTP status 301 (resource permanently moved, important for search engines) and L means stop processing following htaccess stuff

If you know or figure out a better way to establish this, please let me know in the comments.

Posted in apache Tagged with: apache, htaccess