How to Set Up a 301 Redirect In .htaccess With Examples

As a website owner, you must tend to furnish each visitor with a rich user experience.

However due to unavoidable circumstances and an SEO perspective, sometimes it becomes compulsory to restructure websites, even move the entire website to a new domain.

This is where 301 redirects come into play.

In this article, I will provide proper guidance on how to set up a 301 redirect in a .htaccess file from my several years of experience in the SEO and blogging field.

What Is 301 Redirect?

301 redirect is a permanent redirection of one URL to another URL.

301 redirect is an HTTP status code sent by a web server to different browsers.

After the implementation of 301 redirects on a particular URL, it will automatically divert to the new one if users attempt to access the old URL.

301 redirect provides a smooth user experience by redirecting them instantly to a functioning web address.

In addition to redirection, 301 redirection transfers all search engine rankings and authority to the new URL.

It also assists search engines to index URLs properly.

What Is An .htaccess File?

.htaccess is a powerful distributed server configuration file that controls different aspects of a website.

Usually .htaccess (Hypertext access) file is used on the Apache web server and can be utilized to redirect URLs, modify URL structure, and customize error pages.

With the help of the .htaccess file, anyone can change the website configuration file efficiently without editing the server configuration file.

But you should be very careful before editing the .htaccess file.

How To Create An .htaccess File?

You can create a .htaccess file by following these steps:

  • Choose your preferred text editor (Notepad, MS Word, etc.) and create a file naming .htaccess inside your PC folders.
  • You have to name the file exactly .htaccess, otherwise, it will not function properly.
  • Now put in your preferred redirection code and click the ‘Save‘ button
  • Voila! your .htaccess file is ready to go.

How To Upload An .htaccess File?

  • Now you have to upload the .htaccess file using an FTP client.
  • After successfully establishing an FTP connection, you have to simply upload the newly created .htaccess file from your PC to the certain folder of your hosting.

Some Common Uses Of .htaccess File

There are various important use cases of the .htaccess file.

  • The most known and common usage of the .htaccess file is to create URL redirection.
  • With the help of the .htaccess file, you can control to display of either the version or only the version of your website.
  • Can block certain IP addresses from visiting your websites.
  • You can transfer website visitors to HTTP to HTTPS version of your website with the use of .htaccess.
  • You can use the .htaccess file to create a custom 404 error page on your website.
  • .htaccess file helps to password-protect specific folders and files on your server.

Benefits Of 301 Redirect Using .htaccess File

  • .htaccess files can be utilized to redirect individuals to particular pages and websites.
  • This tiny-sized file can handle complex redirection patterns.
  • Easy to create and upload inside the hosting server.
  • It functions with any type of file on your web server.

When Would It Be Necessary To 301 Redirect?

You can use 301 redirects in several circumstances to strengthen website mobility.

  • It’s mandatory to use 301 redirects when you want to change the old URL of a page to a new URL.
  • 301 redirect comes in handy when you want to delete a page and transfer the whole SEO authority and visitor to another page.
  • Willing to change your website domain to a new domain.
  • You have to reverse your website URL from HTTP to HTTPS.
  • You want to add or remove ‘www’ from the website URL.

How To Set Up a 301 Redirect With An .htaccess File?

Usually, 301 redirect using the .htaccess file method are used on Apache servers.

You can also use it on other servers.

How to Set Up 301 Redirect With .htaccess Files In Apache?

To set up 301 redirect with .htaccess files in the Apache server, first you have to locate the Rewrite Engine.

Rewrite Engine allows to edit rules.

You will find the Rewrite Engine in the mod_rewrite module in the Apache server.

Load It Using The Following Code :

<IfModule mod_rewrite.c>

RewriteEngine On


Now you can add all your rules below “RewriteEngine On

The rules you add will depend on what exactly you want to redirect.

Redirecting A Single URL

When you are willing to redirect a single URL, you have to add the following redirect rule under the “RewriteEngine On” line:

Redirect 301 /old-page/

Here we have provided an example.

You must replace those URLs with the actual URLs.

Redirecting A Single Folder

The following 301 redirect .htacces rule will redirect a single folder to a new folder.

RewriteRule ^/?sports/(.\*)$ /tech/$1 [R,L]

In the above redirect rule, I have used two folders “sports” and “tech”.

After successfully implementing the 301 redirect, the “sports” subfolder will be redirected to the “tech” folder.

Redirecting WWW To Non-WWW URLs With A 301 .htaccess Redirect

Use the following redirect rule to implement www to the non-www version of your website.

RewriteCond %{HTTP_HOST} ^ [NC]

RewriteRule (.*)$1 [L,R=301]

Redirect From One File Type To Another Using .htaccess

If you change the extension of your web pages, from .html to .php, with the file names remaining the same, you have to add this inside .htaccess:

RedirectMatch 301 /(.*)\.html$1.php

How To Redirect A Web Page To A Directory Using .htaccess?

To redirect any page to a directory:

Redirect 301 /page.html

Likewise, to redirect a specific directory to another directory:

RedirectMatch 301 /directory

How To Perform 301 Redirect Using .htaccess In WordPress?

Step 1:- Log in using details and click over ‘File Manager‘ in cPanel or FTP client. Here I am using Hostinger.

Redirect 301.htaccess In WordPress (Step-1)
  • Save

Step 2:- In the next, you have to click over the ‘public_html‘ to proceed to the next step.

Redirect 301.htaccess In WordPress (Step-2)
  • Save

Step 3:- Here you will find the .htaccess file. Now right-click on it and select ‘Edit‘.

Redirect 301.htaccess In WordPress (Step-3)
  • Save

Step 4:- Add redirection rule under the ‘RewriteEngine On‘ line.

Redirect 301.htaccess In WordPress (Step-4)
  • Save

Done.. you have completed the steps and the redirection is working absolutely fine.

How To Redirect A Single Page Using .htaccess File?

You have to use the following code to redirect a single page using the .htaccess method on your website.

Redirect 301 /old-page/

In the above redirect rule, you can only mention the URL path in the first part of the rule.

But it’s mandatory to specify the full URL in the second part.

How To Implement A .htaccess File Using FTP?

  1. Create a .htaccess file using a text editor.
  2. Now write or paste your preferred redirection code inside the .htaccess file.
  3. Use your FTP client to access your site.
  4. Make sure to set your FTP client to view hidden files.
  5. Now click over the public_html directory.
  6. Upload your newly generated .htaccess file inside your public_html directory.

How To Redirect An Old Domain To New Domain With .htaccess File?

You have to use the following code to redirect an old domain to a new domain.

With this method, you can redirect the entire site using the .htaccess file.


RewriteCond %{HTTP_HOST} ^(?:www\.)oldsite\.com$ [NC]

RewriteRule ^{REQUEST_URI} [L,R=301]

Here you have to provide actual old and new domain names.

FAQ for Redirect In .htaccess

Q1. What Is The Code For 301 Permanent Redirect?

301 is an HTTP status code sent by a web server to a browser.

Q2. What Is The Rewrite Rule For 301 Redirect?

The Rewrite Rule for a 301 redirect is R=301. It means the redirect will be performed with the particular HTTP status code of 301.

Q3. What Should Be Written In a .htaccess File?

.htaccess file contains one or more configuration directives.

Q4. What Is The Use Of .htaccess File In SEO?

.htaccess file always ensures SEO-friendly redirect. It always confirms that both users and search engine bots are directed to the correct page.

Conclusion on Redirect In .htaccess

Hope I have provided ample input about 301 redirects with a .htaccess File.

However, in addition to the .htaccess method website owners and SEO professionals implement different methods to redirect such as Javascript methods, and server-side code methods.

But 301 redirects using .htaccess are considered as most common and useful ones.

Let me know your preferred redirect method.

Also, don’t hesitate to convey if you are confronting any issues during implementation redirection in websites, I would love to solve your problem.

Hello friends, my name is Sambhu Maity, and I am the founder of DigitalSambhu. Here I share various tips and tricks related to Digital Marketing, Android, Blogging, Social Media, SEO, and various passive income techniques so that beginners can learn everything quickly. If you want to know more details, then definitely click on the "About Us" page.

Leave a Comment

Copy link
Powered by Social Snap