How to redirect any old domain link to exact new domain link with htaccess

It is important to redirect to new domain link after migrating from old domain. More importantly, any old domain links needs to be redirected to new domain link after the migration. The way to do it is to take the help of htaccess file.

So, create a new htaccess file in the document root of the old domain and drop this following code there. 

Without www:

RewriteEngine On

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

With www:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(?:www\.)olddomain\.com$ [NC]
RewriteRule ^ http://newdomain.com%{REQUEST_URI} [L,R=301]

After this redirection step do not forget to change the domain name in google analytics also. Follow this post to change it.