Open In App

Understanding 301 Redirection with its Implementation

Redirection in Computer Science is an essential beneficial service that helps website owners and administrators point their associated domains and subdomains to specific destination URLs when required. Redirection is performed when a specific web-based application is no longer using its original domain for data serving. All users linking to it need to be automatically informed about this inevitable change. So, in this case, Redirection comes into focus. This functionality helps domain administrators to preserve the relevance of the incoming relations to their domain or websites. So in this article, we will be understanding Redirection, 301 Redirection with its Duration, and performing a live demonstration of 301 URL Redirection.

Error Message with Status Code 404

In Data Communication, there are fives groups of status codes that are divided according to the code’s first digit:



  1. 1xx (Information stating codes):  100,101,102,103
  2. 2xx (Request Successful stating codes): 200,201, 202, 203,  204,205 206,  207, 208, 226
  3. 3xx ( Redirection message stating codes): 300,301,302,303,304,305,306,307,308
  4. 4xx ( Error Message Stating code): 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 421, 422, 423, 424, 425, 426, 427, 428, 429, 431, 451
  5. 5xx (Server Encountered error stating codes): 500,501,502,503,504,505,506,507,508,510,511

Let’s understand about 301 redirections in detail.

1. Duration of 301 redirection: Most probably Moved Permanently or 301 Redirections redirect the specified domain or subdomain for approximately a year or sometimes more than a year. You need to check frequently if visitors are being transferred to your newly created URL( after a year). .htaccess plays an important role in direction. If you want to remove the redirection, you can modify the .htaccess file and remove the redirection, so afterward, the website won’t forward users to the new URL anymore.



Understanding 301 redirection via diagram: 

301 Redirect from Old Page to New Page

From the above figure, the old page of the domain is redirected or moved permanently to the new page due to some internal application issue.

2. When to Perform 301 Redirection: 301 redirects are commonly used when:

  1. Domain owner wants to change a page’s URL.
  2. Domain owner wants to connect pages about the same topic.
  3. Domain owners have to change a subfolder’s URL (e.g., https://geeksforgeeks.org/old-directory/ to https://geeksforgeeks.org/new-directory/).
  4. Domain owners have moved a subdomain to a subfolder (e.g., https://demo.geeksforgeeks.org to https://geeksforgeeks.org/demo/).
  5. Domain owners have to change domain names (e.g., geeksforgeeks.org to geeksforgeeks.in).
  6. Domain owners have to switch from HTTP to HTTPS (e.g., http://geeksforgeeks.org to https://geeksforgeeks.org).

3. Technique to Implement 301 Redirection using .htaccess configuration file: htaccess is a configuration file for use on web servers running the Apache Web Server software. This file is actually responsible for Redirection.

Now let’s see the steps to be followed to implement 301 redirections: 

redirect 301 /old/old_page.html http://www.demoexample.com/new_page_redirection.html

Example: In the below example, we are going to perform 301 Redirection. For this demonstration, we are hosting our files on LocalHost, i.e. XAMPP Server.

We have created two web pages named: 

  1. OLD_PAGE.html
  2. NEW_PAGE_REDIRECTED.html

1. OLD_PAGE.html: In the OLD_PAGE.html web page, we will redirect this page to NEM_PAGE_REDIRECTED.html by adding the 301 Redirection code in the .htaccess file. .htaccess file is the configuration file for a web-based application. .htaccess file can manage all the internal structures through this file. The OLD_PAGE.html is the webpage that web application will redirect to another web page. When any user enters the OLD_PAGE.html URL, they will be automatically redirected to the New redirected page.

Filename: OLD_PAGE.html




<!DOCTYPE html>
<html>
   <head>
      <title>OLD PAGE</title>
   </head>
   <body>
      <h2>WELCOME TO OLD PAGE</h2>
       
<p>THIS IS THE PAGE WHICH WE ARE GOING TO REDIRECT TO ANOTHER PAGE</p>
  
   
   </body>
</html>

Output:

Fig 1 . THIS IS OLD_PAGE.html OUTPUT

2. NEW_PAGE_REDIRECTED.html: To perform the redirection, we need the destination webpage from which our users will be sent from the old page to the new page. So our destination page or New Redirected page is NEW_PAGE_REDIRECTED.html. If the user enters the OLD_PAGE.html URL, then he will automatically switch to NEW_PAGE_REDIRECTED.html URL.

Filename: NEW_PAGE_REDIRECTED.html




<!DOCTYPE html>
<html>
   <head>
      <title>THIS IS NEW PAGE [REDIRECTED PAGE]</title>
   </head>
   <body>
      <h2>THIS IS THE NEW PAGE WHICH HAVE CAME FROM OLD_PAGE REDIRECTION</h2>
       
<p>WE ARE IN OLD PAGE</p>
  
   
   </body>
</html>

Output:

Fig 2. THIS IS NEW_PAGE_REDIRECTED.html OUTPUT 

3. .htaccess file for Redirection(Configuration file): As We stated above, .htaccess is the file responsible for any internal structure of any type of Redirection Feature. So owners should handle this file carefully. In this file, we need to add the code of the 301 Redirection. The code is just one line code in which we need to specify the old page and the new redirection page URL. This single line code is responsible for the 301 Redirection in the web-based application. As per the need, you can redirect number of various pages only you need to add the code in .htaccess file.

Note: 

1. Make sure the file should be saved as .htaccess without extension. As in Geeksforgeeks Code Embed Editor there is no empty extension facility , I have added the source code in the HTML section but at your side you need to save it without any extension.

2. The # Added sentences are only for Readers Understanding, no need to add this lines while performing practical at your side.

.htaccess




#.htaccess file content
#OLD_PAGE.html will be Redirected to NEW_PAGE_REDIRECTED.html Page Automatically

Output:

Video 1. OUTPUT VIDEO (LIVE DEMONSTRATION)

 Explanation: As we enters the OLD_PAGE.html URL in the address bar the Web Application is automatically taking us to the other URL, and this Other URL is of NEW_PAGE_REDIRECTED.html page. So this is due to the 301 Redirection Code which is Written in the .htaccess file. Although copying the http://localhost:8081/OLD_PAGE.html link and pasting it to new tab will always redirect us to  http://localhost:8081/NEW_PAGE_REDIRECTED.html Page, till we remove the Redirection Code from .htaccess file. You can see in above output, I was trying to open http://localhost:8081/OLD_PAGE.html but as we have done redirection we were redirect to http://localhost:8081/NEW_PAGE_REDIRECTED.html.

This redirection will be lasting till the owner removes the redirection command from the .htaccess file. 


Article Tags :