Setting Up a Permanent 301 Redirect in .htaccess

To redirect an entire site using HTAccess

If you have a website, then you want to go to a new domain, it is one of the easiest and best ways of doing it with your 301 redirect in .htaccess file in the root server.

301 redirects are important

It is important that you use 301 redirects instead of meta refreshes or other types of redirects. It tells search engines that pages have been permanently transferred to a new location. Google and other search engines will then update their index to use new domains without changing their indexing values.

Therefore, if your old website is doing very well on Google, then this good ranking will continue after the redirect is indexed. I have personally used 301 redirects for many pages on this site, which have not changed in rankings.

Difficulty: Average
Time required: 15 minutes
Put all of your content on the same domain as the old domain, using the same domain structure and file names. This is the most important step. To redirect this 301 to work, the domain should be similar in the file structure.
You may also consider entering a noindex, nofollow robots.txt file on this new domain until you set up the redirect. This will ensure that Google and other search engines do not index other domains and punish you for duplicate content. But if you do not have too much content or can get all the content copied in one day, then it is not as important.

On your old domain website, open .htaccess file in your root directory with a text editor – if you do not have a file called .htaccess (note the front point), create one. This file can be hidden in your directory list.

Add line: redirect the // com / // domain / com in the hhacaccess file at the top.

Change the URL // to the new domain name that you are redirecting to.

Save the file to the root of your old website.

Test that the old domain pages now point to new domains.

Manual way to set up a 301 Redirect

Redirect individual files

One page:
Redirect 301 /oldpage.html

Redirect individual files (Multiple pages, each on it’s own line)

Redirect 301 /oldpage1.html
Redirect 301 /oldpage2.html
Redirect 301 /oldpage3.html
Redirect 301 /oldpage4.html

Redirect an old domain to a new domain (Wild card way to set up a 301 redirect)

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*)$1 [R=301,L]

Force www. version of domain to be used

RewriteEngine on
RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule ^(.*)$$1 [L,R=301,NC]

Force non www. version of domain to be used

RewriteEngine on
RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule ^(.*)$$1 [L,R=301,NC]

Redirect all files with certain extension

RewriteEngine On
RewriteCond %{REQUEST_URI} .php$
RewriteRule ^(.*).php$ /$1.htm [R=301,L]