What is a 301 Redirect? And how to setup it in htaccess file?

Question: What is a 301 Redirect? And how to setup it in htaccess file? If you are a Website Owner, Web Developer, or Webmaster, there are certain times when you need to move your website from one URL to another.

And you want to ensure all of your visitors reach the correct URL as well as Search Engine Robots.

In the tech world, this is called 301 Redirect, 301 Moved Permanently, or Permanent Redirect. If you are very concerned about the SEO of your website, then 301 Redirect is very useful to stop any unseen content duplication.

A 301 Redirect is your best friend in the World of Web as it keeps your hard-earned Search Engine Ranking when the site’s URL is changed for any reason. It seamlessly sends search engines and the visitors to the new URL, although the requested URL is an old one.

In this guide, you will learn:

  • What is a 301 Redirect?
  • How to setup 301 Redirect in .htaccess file?


What is a 301 Redirect?

Sometimes back, there was a very detailed article about “What is a 301 Redirect?” by Network Solutions on Google Search but now it is moved.

Network Solutions 301 redirect

Never mind, I will try to explain what is a 301 Redirect and how to setup to redirect your website URLs without losing any of your SEO done.

The 301 Redirect or status code 301 Moved Permanently means a webpage is moved permanently from the old URL to a new URL.

It will redirect a visitor and search engine to the new URL once they type the old URL in the browser or click from the search engine result page.

To understand it, Let’s say you type blog.iadnanahmed.com in the browser address bar and hit enter, it will be redirected to iadnanahmed.com/blog with status code 301, telling the browser the site URL is moved permanently to the new one.

To achieve this, I simply did a redirect in the .htaccess file as in the screenshot below.

.htaccess iadnanahmed.com blog redirect

And to simply verify everything is working as expected, there are few extensions for different browsers, but I use Redirect Path for Google Chrome, which shows redirect path as below.

Redirect Path Chrome Extension


How to setup 301 Redirect in .htaccess file?

Now the question comes, “How to setup 301 redirect in .htaccess file?” Just follow my guide below which will help you to solve this problem.

If your website is PHP based or WordPress website, the method of doing redirect in .htaccess is the same.

First of all, you need to create a new .htaccess file if not already there. Use a file manager on your cPanel or Plesk hosting or any popular website manager to create or update.

In the case of cPanel hosting:

First click on the File Manager iconFile Manager in cPanel

The second step is to go to the public_html folder.public_html directory

Create a new .htaccess file if it is the first time; otherwise, you can edit the existing one.

htaccess file


Redirect a single old page to a new page

The first type of redirect is the single old page redirect to a new page using the .htaccess file. For example, a page name is changed, or a new page with better content.

Redirect 301 /the-old-page.html /a-new-page.html


Migrate and Redirect your old domain to new domain

Sometimes you as a business need to rebrand your business, and to reflect a new business name needs to change the domain name also.

In this case, the entire domain will be redirected to the new domain name, and the following code in the .htaccess file is one way of doing it.

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


Redirect whole domain from HTTP to HTTPS

In case you had installed SSL on the website and website is serving from HTTPS instead of HTTP, it is still necessary you redirect all HTTP requests to HTTPS.

This way, Google will only index the correct version of your website.

The code to do HTTP to HTTPS redirect is below.

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


Redirect whole domain from non-www to www (and reverse)

And also, now, to keep a single version of the website indexed in Google, it is necessary to either redirect all URLs to www or non-www.

Let’s say your choice to keep www as your preferred version in Google. The code in the .htaccess

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


Redirect whole domain from both non-www to www and HTTP to HTTPS

If you want to consolidate both of the above codes in one single piece of code, i.e., HTTP to HTTPS and www to non-www, you need to put this in the .htaccess.

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Your website will be redirected to https://www.yourwebsite.com/ by putting this code, and Google will keep a single version of the site indexed, and the customers will found this website in the browser also.


And how to do a 301 redirect in PHP?

What if you want to do the same in the PHP file instead of .htacces file?

A 301 redirect for non-www URLs to www, simply use PHP header command like the following syntax:

 if ($_SERVER['HTTP_HOST'] != 'www.abc.com'){ header("HTTP/1.1 301 Moved Permanently", true, 301); header("Location: http://www.abc.com".$_SERVER['REQUEST_URI']); } 

And for www to non-www:

if ($_SERVER['HTTP_HOST'] != 'abc.com'){ header("HTTP/1.1 301 Moved Permanently", true, 301); header("Location: http://abc.com".$_SERVER['REQUEST_URI']); } 

It is handy where you don’t want to use the .htaccess file. But remember to put the code on all pages where you required the redirect.

I hope this brief guide will give you an overlook of multiple types of 301 redirects .htaccess.

I will welcome any comments and questions below.

Load WordPress Sites in as fast as 37ms!

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest
© Copyright 2008 – 2024 Adnan Ahmed – Freelance WordPress Web Developer


Get the latest updates in your inbox!