How To Use Htaccess File To Redirect Website - FreeCodeBlog

Last updated: Dec 15, 2021

Do you have any websites on the live server that need redirection? Yes. Did you find a duplicate version of the website? Yes. Then you need to use the .htaccess file to fix the duplicate website URL problem. You can easily use htacces file to redirect website to anywhere. You need to use htaccess file to redirect your website to https, http, www, non-www, subfolder, other-website. You can create and write rewrite rule to redirect your website. Also you need to use htaccess rewrite rule to prevent duplicate error. Are you ready to use htaccess file to redirect website? Yes. Okay don't be late. Just start to read this tutorial.


How To Use Htaccess File To Redirect Website
How To Use Htaccess File To Redirect Website


You may encounter multiple / duplicate URL issues after uploading your website to the live server. So you need to create htaccess and rewrite the rules to fix the URL of multiple / duplicate websites. In this tutorial I will explain to you how we can solve all the problems related to URL using htaccess rewrite rule.


1. Duplicate / Multiple URL Means.

Suppose you have a website called "". Now you can access your website using 4 different like https://, http://, https://www., http://www. URL. See below, you can use 4 different URLs for the same domain. Don't want to use multiple URLs for the same domain now? Want to redirect 3 URLs to 1 URL? You need to use htaccess rewrite rule to redirect 3 URLs to 1 URL.


2. Htaccess File.

Want to redirect your website to a URL second URL? First you need to create an .htaccess file in the root folder of your file manager (public_html or others) to redirect the website. For cpanel you can easily create an .htaccess file by clicking on the new file from the file manager navbar. I suggest you create an .htaccess file in the root folder (public_html) to redirect the website.


3. Redirect NON-WWW To WWW.

If you want to redirect your non-www website to www then use below code manually in .htaccess file.


RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain-name\.domain-extension$
RewriteRule ^(.*)$ "https\:\/\/www\.domain-name\.domain-extension\/$1" [R=301,L]


4. Redirect To Subfolder.

Suppose if have a website files that store on public_html/subfolder. Now you want, when somebody hit enter your domain ( on any browser the domain ( will redirect your subfolder to access files.


RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain-name.domain-extension$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain-name.domain-extension$
RewriteCond %{REQUEST_URI} !subfolder/
RewriteRule (.*) /subfolder/$1 [L]


5. Cpanel Redirect Option.

Do you use cpanel for your website? Cpanel has a redirect feature that can redirect your website URL. Login to the cpanel dashboard and find the redirect option in the cpanel domain section. If you do not want to use the redirect code manually in the .htaccess file, you can easily use the redirect option to redirect your website. Make sure you have a created htaccess before using this redirect.


Okay so this is for today. See you again next tutorials. Thanks

Post Tags
Post Writer
Masum Biswas (@masum7323)

My name is Masum Biswas. I'm a full stack developer. I live in Bangladesh and I love to write tutorials and tools that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, MySQL, VueJS, Tailwind CSS and Bootstrap from the early stage.

Follow Me On
Related Posts
How To Deploy A Laravel 8 Project On Namecheap Shared Hosting Cpanel

Today I will explain you, how we can easily deploy a laravel 8 project on namecheap shared hosting cpanel. If you have a namecheap shared hosting s... Read More

Deploy Laravel Via Ftp On Shared Hosting Cpanel

Deploy laravel via ftp on cpanel! Want to deploy laravel on shared hosting cpanel via ftp? Yes. But why? Because I can't access cpanel public_HTML... Read More

Deploy Laravel Project To Shared Hosting Cpanel Using Zip File

Want to deploy laravel project to cpanel using zip file? Yes. Okay don't worry friends, You can easily deploy any kind of laravel project to shared... Read More

How To Check And Change Php Version On Shared Hosting Cpanel

Want to check PHP version on cpanel? Yes. Want to change PHP version on cpanel? Yes. Want to deploy PHP script on cpanel? Yes. Okay before deploy P... Read More

The Best Way To Upload Laravel Projects To Shared Hosting Cpanel

We can easily upload laravel projects to shared hosting (cpanel) using git version control. Git version control (Private Repository) is the best wa... Read More