301 Redirect – How To Create A 301 redirect Properly Complete Guide


Many times, we face an error in our Google Search Console that is “page not found”. This happens for some reasons if you delete any page or content or if you edit your post title and URL. This page not found error might harm in your Google ranking. However, the good news is you can fix this problem using a 301 redirect.

In this tutorial, we will learn “How to create a 301 redirect properly”. Where and when we should use a 301 redirect on our website.

So, let’s get started

Read Also: How to Backup & Restore website with UpdraftPlus.

 

What is a 301 redirect?

A 301 redirect is a permanent redirect process from one site to another or one page to another page. This is an implementation of code to send your users from one URL to another automatically.

This 301 redirect helps to reduce errors and give the required information to the users. Now the question is when should we use a 301 redirect?

 

When should we use a 301 redirect?

We already know a 301 redirect is a permanent redirect. There are many situations where we use a 301 redirect. I am showing you some of them, which are really important to know.

  1. If you want to change your domain name for some reason, you must use a 301 redirect. Otherwise, you will lose your users of the old domain name. This will redirect your old users to your new domain name.
  2. You can redirect your traffic from an outdated page to the desired page. In this way, you will not lose all those potential traffic as well as it will increase your website or brand name value.
  3. You can redirect from http:// to https:// or in www. Version of your domain using a 301 redirect very easily.
  4. To redirect traffic to the website from other URLs owned by the same person or organization.

 

Some uses of a 301 redirect

Now we learn how we can use a 301 redirect for some use case. For that, you have to write some code. However, don’t worry a backup for you. I will provide you all the necessary codes.

First, log in to your cpanel by typing your domain name with /cpanel at the end. For example https://domainname.com/cpanel

Because you are going to use .htaccess to create a 301 redirect. Find your “File Manager” option and go inside.

cpanel file manager

 

Now click on the “public_html” folder. If your website is installed in the root folder then you will find all the files inside this public_html folder. Otherwise, go to the specific folder inside public_html where your website is installed.

 

go to public_html

 

Now find out “.htaccess” file in this folder. If you don’t find it that’s ok. Click on “Settings” at the top right corner.

This will open up a new popup. Here you have to select “Show Hidden Files(dotfiles)” and click on save.

show hidden files

 

Now probably you will find a .htaccess file if you have one. If it does not show up then create the .htaccess file by clicking “File” option at the top left corner.

Right click on your .htaccess file then click “Edit”. This will open up a new popup. In this popup, you have to click “Edit” again. Now you will get your .htaccess file opened on a new tab.

 

open htaccess file

 

You have to write some code inside this file to create a 301 redirect. It depends according to your need. I am going to show you 5 most important uses of a 301 redirect with their codes.

  • Redirect individual files on the same domain
  • Redirect an old domain to a new domain
  • Force www. version of the domain to be used
  • Force non www. version of the domain to be used
  • Redirect all files with a certain extension

 

# Redirect individual files/pages

To redirect individual files and pages, you can use a 301 redirect. Like, if I want to redirect https://worldtechis.com/oldpage.html to https://worldtechis.com/newpage.html, I can do this by using the following code inside .htaccess file.

 

Redirect 301 /oldpage.html https://worldtechis.com/newpage.html

 

Here, I will give the ending part first (/oldpage.html). Then I have to give the URL of the new page. You have to use our page URL here.

Now click on “Save changes” button at the top left corner to save your .htaccess file.

You can type your old page URL in your browser to see the change. I believe this will redirect you to the new page.

 

# Redirect from old domain to a new domain

If you have an old domain name like olddomain.com and you want to redirect this to a new domain newdoamin.com, you can easily do this using a 301 redirect.

Use the given code. Copy and paste it inside your .htaccess file.

 

RewriteEngine on

RewriteCond %{HTTP_HOST} ^olddomain.com [NC,OR]

RewriteCond %{HTTP_HOST} ^www.olddomain.com [NC]

RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301,NC]

 

Here, you just need to replace olddomain.com and newdomain.com with your domain names. In addition, don’t forget to save your .htaccess file after doing all these changes.

 

# Force www. version of the domain

If your domain name does not contain www at the beginning like example.com, but you want to use www like www.example.com

You can do this by a 301 redirect. Copy and paste the following code inside your .htaccess file.

 

RewriteEngine on

RewriteCond %{HTTP_HOST} ^example.com [NC]

RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]

 

Just change example.com to your domain name. This will add www at the beginning to your domain name. Save your .htaccess file and see the change.

 

# Force non www. version of the domain

As we have seen earlier that we can use www at the beginning of our domain if we don’t have it. We can also use non www version if we have it in our domain.

To remove www from your domain you have to use the following code in your .htaccess file.

 

RewriteEngine on

RewriteCond %{HTTP_HOST} ^www.example.com [NC]

RewriteRule ^(.*)$ http://example.com/$1 [L,R=301,NC]

 

Use your domain name instead of example.com. This will work fine. Save .htaccess file and see the change.

 

# Redirect all files with a certain extension

You can also change the file type using a 301 redirect. For example, if you have files with .html extension at the end you can easily change to the .php extension.

Use the given code below if you need to change the file type. Copy and paste it into your .htaccess file.

 

RewriteEngine On

RewriteCond %{REQUEST_URI} .html$

RewriteRule ^(.*).html$ /$1.php [R=301,L]

 

We are changing .html to .php file. You can also do the reverse. Just use .php instead of .html and .html instead of .php

 

Conclusion

We have learned what a 301 redirect is and when we should use it. I hope will give you a clear idea. I have also given codes for 5 use cases. You can use them according to your need.

A 301 redirect is very important to SEO. If Google does not find any page then it will create a bad impression on your website. I will highly suggest you use a 301 redirect.

So if you need any assistance with this tutorial, leave a comment below and I will try to help you out!! I will look forward to your opinion in the comments section below.

You can subscribe to our newsletter. I always provide the latest update about my new contents that may help you. Please share this post with your friend if it is helpful for you. So that they can also learn How to create s 301 redirect properly for their websites.

 

You may also like:

⇒ How to become a full stack developer? – [Salary and Skills]

 Top 10 programming languages of the future choose the right one

⇒ How to Install WordPress on cPanel Right Way [A to Z]

⇒ How to backup WordPress Site & Move to New Host

⇒ Yoast SEO Tutorial | A-Z Guideline for Beginners



I am Robin. Founder of "WorldTechis" community. It is the Largest online learning platform, here you can learn Web development, programming and advance Wordpress tips and tricks. As a professional developer, here I provide all necessary tips related to Programming & Wordpress. Like my Facebook page WorldTechis for the latest update or contact me - [email protected]

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *