Add

How To Redirect A Website Using .htaccess File

Introduction

.htaccess file is a small text document that generally presents in the same location of the default page of your site. Its a very powerful file unlike the config file, a small mistake can make your whole site down. So before doing any changes to the .htaccess
file you should be very careful.

There are various things you can do with the .htaccess file. Here I will explain some custom redirects using .htaccess file.

Behind The Scene

Using .htaccess we can acheive numerous custom redirections as below.

Redirect Permanently


301 (Permanent) Redirect: If you have shifted your website from one domain to another, and you want to divert all the requests to the older site to the new one then do it as below:
     
 # This allows you to redirect your entire website to any other domain
 Redirect 301 / http://www.tapankumar.in/


The above command will redirect you to www.tapankumar.in.

Redirect Temporarily


302 (Temporary) Redirect: Point an entire site to a different temporary URL. This is useful when you have a temporary landing page and plan to switch back to your main landing page at a later date:

     
 # This allows you to redirect your entire website to any other domain
 Redirect 302 / http://www.tapankumar.in/



Redirect To a Specific Sub-Folder


In case you want to redirect from your index.html page to another sub directory then you need the below command in your .htaccess file.

     
 # This allows you to redirect index.html to a specific subfolder
 Redirect /index.html http://www.tapankumar.in/newdirectory/


The above command will redirect you to www.tapankumar.in/newdirectory/.

Redirect To a Specific Page


In case you want to redirect from your index.html page to another page called home.html, then you need the below command in your .htaccess file.

     
# Redirect old file path to new file path
Redirect /olddirectory/index.html http://www.tapankumar.in/newdirectory/home.html
 

The above command will redirect you from olddirectory/index.html to newdirectory/home.html page.

Redirect To a Specific Index Page


You can set your default page of your site in the IIS server or in the apache server. but by using your .htaccess file you can set your default page by your own

     
# Provide Specific Index Page (Set the default handler)
DirectoryIndex index.html
 

The above command will set index.html page as your default page.

Once again I need to say, while working with .htaccess file be careful or else it will break your whole system.

Happy Coding...

No comments:

Post a Comment