301 redirect by .htaccess

How can I 301 redirect my website using a .htaccess file?

If you change your website or company URL or just change link to some specific page and you want all your visitors or search traffic to follow you to the new URL. Then you can use some of the following options on a Linux Server.
301 redirect
Redirect an old domain to a new domain

If you had an old domain such as example.com, and now you decided you actually want to use example.net for the website. You could setup a 301 redirect for the entire domain, so that old links to example.com carry over.

Code in the example.com domain’s .htaccess file:

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

Force www. version of domain to be used

A search engine like Google would see example.com and www.example.com as essentially two separate websites. They recommend you pick one version you’d like search engines to display and using a 301 redirect is a possible option.

If you have a lot of links on the web where people are linking to your site as example.com, but you would like your visitors to instead end up at www.example.com you can force this version of your domain with these rules:

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