Web Hosting

How to Setup 301 Redirect via .htaccess?

Written by Akash Navi

If you have changed the URL or address of any post or file of your website. Then you have to redirect the traffic that is coming to the old URL to the new URL. If you don’t redirect the traffic then the visitors and the search engine bots who visit your website will get a 404 error. You can redirect the traffic if you setup 301 redirect via .htaccess file.

This is considered as the best method to setup 301 redirect for your website. But this is not the only method by which you can redirect the traffic from your old URL to new URL.

You can do that by forwarding your domain name too. You can setup 301 permanent redirect in your domain name’s DNS manager.

But you can use the DNS manager method to redirect the traffic only if you are changing the address of your whole domain name.

If you want to redirect only a few URL’s of your website then you can do that by setting up 301 redirect in .htaccess file.

Before we proceed to the further part of this guide and learn how you can setup 301 redirect via .htaccess let’s check out what exactly .htaccess file is.

If you don’t know about the .htaccess file then you can read it below.

What is .htaccess file?

.htaccess is a configuration file for use on web servers running the Apache Web Server software. When a .htaccess file is placed in a directory which is in turn ‘loaded via the Apache Web Server’, then the .htaccess file is detected and executed by the Apache Web Server software. These .htaccess files can be used to alter the configuration of the Apache Web Server software to enable/disable additional functionality and features that the Apache Web Server software has to offer. These facilities include basic redirect functionality, for

These .htaccess files can be used to alter the configuration of the Apache Web Server software to enable/disable additional functionality and features that the Apache Web Server software has to offer. These facilities include basic redirect functionality, for

These facilities include basic redirect functionality, for instance, if a 404 file not found error occurs, or for more advanced functions such as content password protection or image hotlink prevention.

How to Setup 301 Redirect via .htaccess file of Your Website?

Setup 301 Redirect via .htaccess file

Now we know what is .htaccess and what are the things we can do with the help of .htaccess file. Let us get started with this guide and check out how we can setup 301 redirect via .htaccess file for your website.

In this guide, I will tell you how you can add various types of 301 redirect via .htaccess which are mentioned below.

  • Redirect individual files on the same domain name.
  • Redirect traffic from one domain name to new domain name.
  • Force to use www. or non-www. version of your domain name.

Where Can You Find .htaccess File and How to Edit it?

To setup 301 redirect via .htaccess file, you have to edit the file. You can not alter the file without editing it.

This file can be found on your web server where you have hosted your website. The .htaccess file of your website can be found in the home directory of your website.

To find this file all you have to do is access your cPanel and heading towards the home directory of your website. If you can’t see any file by name .htaccess over there. Then you have to enable the display of dot files on your server.

Even the .htaccess is a dot file and that is the reason why it has a dot before its name.

Click on the setting button which you can find at the upper right corner of the file manager in cPanel interface.

Show dot files in cPanel file manager

Tick Show Hidden Files (dotfiles) and click on save, and you will be able to see the .htaccess file in the file manager.

editing .htaccess file

If you want to edit the .htaccess file all you have to do is right-click on the file name and click on Edit and you will be allowed to edit the file.

Redirecting Individual Files on the Same Domain Name

If you want to redirect only a few files of pages of your website within the same domain name. You can use follow this method.

You have to enter the code mentioned below in your .htacess file. To edit and enter the code into the .htaccess file you can have to follow the same procedure which is mentioned above. (to find and edit the file)

Redirect 301 /oldurl /newurl

Replace the oldurl with your current URL and the newurl with the URL to which you would like to redirect the traffic.

Redirecting Traffic from one Domain Name to New Domain Name

If you want to redirect the whole traffic you receive from your current domain name to any new domain name. Then you have to add the code mentioned below in your .htaccess file.

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

Here you have to replace currentdomain.com with your domain name which you are currently using. And http://newdomain.com/ with the new domain name along with the protocol. (http or https)

Forcing Use of www. or non-www. Version of your Current Domain Name

This redirection is used when you want to force the use of www. or non-www. version of your domain name. For example, if you currently you are using www.example.com.

And want to use example.com then you can use this code and same for the vice versa.

To Force the Use of www. Version:

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

Replace the example.com with your domain name.

To Force the Use of non-www. Version:

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

Replace the example.com with your domain name.

So this was all about how you can setup 301 redirect via .htaccess file of your website. I hope you are clear with all the steps and information mentioned in this guide.

But if you have any kind of doubts in your mind then you can comment below. I will try my best helping you to setup 301 redirect via .htaccess file. But you have to keep one thing in your mind and that is before editing your file make sure you have a backup with you. If something goes wrong while editing the file your website may go down.

2 Comments

Leave a Comment

How About Subscribing to Blogging Champs?

How About Subscribing to Blogging Champs?

Subscribe & Receive The Best in Class Blogging Guides and Case Studies via Mail

You have Successfully Subscribed!