AEM 301 & 302 redirects

In this blog post, we will discuss the significance of 301 redirects, exploring the reasons behind their necessity and providing insights into how to effectively implement redirect conditions and rules. We will delve into the purpose of 301 redirects, examining why they are essential in various scenarios. Additionally, we’ll guide you through the process of setting up and managing 301 redirects, offering practical tips and considerations to ensure a seamless and optimized web experience.

A 301 redirect also called as permanent redirect enables users to be automatically redirected to a new URL when they access the old URL in their web browser. For instance, if a user attempts to visit the old URL ‘https://javadoubts.com/aem-dispatcher-setup/‘, they will be seamlessly redirected to the new URL ‘https://javadoubts.com/aemaacs-dispatcher-configuration/’—the designated replacement. This redirection process helps maintain a smooth transition for users and ensures they reach the updated content or location without any manual intervention.

Why do we need 301 redirects? 

A 301 redirect becomes necessary when an old URL no longer exists, often due to the deletion of a page or changes in the site’s hierarchy. If a user attempts to access such a non-existent URL — either directly or through a bookmark — they would typically be led to a 404 error page, which can be a frustrating experience. To prevent this and ensure a seamless user experience, a 301 redirect is implemented. This redirect automatically guides users to a valid, existing page. By doing so, it not only enhances user experience but also maintains the integrity of the website’s navigation and search engine rankings.

Note: 

  • It is essential to ensure that 301 redirects are properly managed, especially when dealing with content delivery networks (CDNs). Ideally, the redirection should be configured directly through the CDN. However, in cases where the CDN does not support or allow the necessary redirect settings, it becomes necessary to implement the 301 redirect through the web server, such as using the dispatcher. This ensures that the redirection is handled seamlessly, maintaining a consistent and user-friendly experience even when CDN restrictions are in place.
  • In case of 301 redirect, SEO remove old pages from its search index.

Implementation

Create legacy_rewrite.rules file within /dispatcher/src/conf.d/rewrites dispatcher module to segregate all 301 redirect rules as part of it. Include legacy_rewrite.rules as part of javadoubts_rewrite.rules as shown below: 

Below 301 redirect rewrite condition will verify if URL is starting with /getattachment. Rewrite rule will allow us to redirect to /en/attachment/first if URL is starting with /getattachment

Pow(^) symbol is for verifying if URL starts with. 
e.g. ^/attachment -> It talks about if URL starts with /attachment.

Asterisk(*) symbol is for matching a regex.  
e.g. ^/attachment* -> It talks about if URL starts with /attachment and have some other values post attachment like /attachment, /attachments, /attachment-en, etc.

RewriteCond %{REQUEST_URI}  ^/attachment*
RewriteRule /attachment*    /en/attachment/first [R=301,L]

Below is just an example for a rewrite condition if URL starting with /en/practice-language OR /fr/practice-language OR /de/practice-language.

Below rewrite rule will 301 redirect from /en/practice-language-en to /en/language

$ is nothing but to match if URL ends with which is a kind of one to one mapping.

RewriteCond %{REQUEST_URI}  ^/(en|fr|de)/practice-language*
RewriteRule /(en|fr|de)/practice-language-en$ /$1/language [R=301,L]
RewriteRule /(en|fr|de)/practice-language-en$ /$1/language [R=301,L]

If URL starts with /it-IT than redirect it to /it

RewriteCond %{REQUEST_URI}  ^/it-IT/*
RewriteRule ^/it-IT/*  /it [R=301,L]

Below redirect is kind a one to one mapping which will check for exact match. In below example it will check for exact URI /gender/male and redirect to /en/generic

RewriteRule /gender/male$  /en/generic [R=301,L]

Below is an example for 301 redirect where user will get redirect depending on host name or domain.

e.g. request coming from domain www.javadoubts.com will 301 redirect to https://www.javadoubts.com/en/javadoubts

RewriteCond %{HTTP_HOST}    www.javadoubts.com
RewriteRule ^/?$ "https://%{HTTP_HOST}/en/javadoubts" [L,R=301,NC,QSA,NE]

RewriteCond %{HTTP_HOST}    www.practice.com
RewriteRule ^/?$ "https://%{HTTP_HOST}/en/practice" [L,R=301,NC,QSA,NE]

Verification

Use Link to setup a dispatcher using Apache Web Server or URL to setup dispatcher using docker to test 301 redirect changes in local.

Copy paste below lines under httpd.conf file. 

Note: You may have different rules depending on project and site hierarchy.

<IfModule mod_rewrite.c> 
 <VirtualHost *:80>  
  <Directory "C:/Users/user/Documents/apache_cache">
   <IfModule disp_apache2.c>
    ModMimeUsePathInfo On
    SetHandler dispatcher-handler
   </IfModule>
   
   Options FollowSymLinks Includes
   AllowOverride None
   AddOutputFilter INCLUDES .html
  </Directory>
 
  ServerName practice.abc
  
  ProxyRequests off
  ProxyPreserveHost On

  LimitRequestFieldSize 32768
  AllowEncodedSlashes NoDecode
  RewriteEngine On
 
  # Rules 1
  RewriteCond %{REQUEST_URI}  ^/attachment*
  RewriteRule /attachment*    /en/attachment/first [R=301,L]

  # Rules 2
  RewriteCond %{REQUEST_URI}  ^/(en|fr|de)/practice-language*
  RewriteRule /(en|fr|de)/practice-language-en$ /$1/language [R=301,L]
  RewriteRule /(en|fr|de)/practice-language-en$ /$1/language [R=301,L]

  # Rules 3
  RewriteCond %{REQUEST_URI}  ^/it-IT/*
  RewriteRule ^/it-IT/*  /it [R=301,L]

  # Rules 4
  RewriteCond %{HTTP_HOST}    www.javadoubts.com
  RewriteRule ^/?$ "https://%{HTTP_HOST}/en/javadoubts" [L,R=301,NC,QSA,NE]

  RewriteCond %{HTTP_HOST}    www.practice.com
  RewriteRule ^/?$ "https://%{HTTP_HOST}/en/practice" [L,R=301,NC,QSA,NE]

 </VirtualHost>
</IfModule>

302 Redirect

A 302 redirect is to temporarily replace old URL for time being to test new site changes. If a user attempts to access such URL, they typically be led to new URL. This redirect automatically guides users to a new valid, existing page. 

To achieve 302 redirects, in the above examples we can replace 301 with 302 as shown below: 

<IfModule mod_rewrite.c> 
 <VirtualHost *:80>  
  <Directory "C:/Users/user/Documents/apache_cache">
   <IfModule disp_apache2.c>
    ModMimeUsePathInfo On
    SetHandler dispatcher-handler
   </IfModule>
   
   Options FollowSymLinks Includes
   AllowOverride None
   AddOutputFilter INCLUDES .html
  </Directory>
 
  ServerName practice.abc
  
  ProxyRequests off
  ProxyPreserveHost On

  LimitRequestFieldSize 32768
  AllowEncodedSlashes NoDecode
  RewriteEngine On
 
  # Rules 1
  RewriteCond %{REQUEST_URI}  ^/attachment*
  RewriteRule /attachment*    /en/attachment/first [R=302,L]

  # Rules 2
  RewriteCond %{REQUEST_URI}  ^/(en|fr|de)/practice-language*
  RewriteRule /(en|fr|de)/practice-language-en$ /$1/language [R=302,L]
  RewriteRule /(en|fr|de)/practice-language-en$ /$1/language [R=302,L]

  # Rules 3
  RewriteCond %{REQUEST_URI}  ^/it-IT/*
  RewriteRule ^/it-IT/*  /it [R=302,L]

  # Rules 4
  RewriteCond %{HTTP_HOST}    www.javadoubts.com
  RewriteRule ^/?$ "https://%{HTTP_HOST}/en/javadoubts" [L,R=302,NC,QSA,NE]

  RewriteCond %{HTTP_HOST}    www.practice.com
  RewriteRule ^/?$ "https://%{HTTP_HOST}/en/practice" [L,R=302,NC,QSA,NE]

 </VirtualHost>
</IfModule>

Note: In the case of a 301 redirect, search engines like Google typically remove the old pages from their search index because a 301 redirect signals a permanent move of the content to a new URL. However, this does not occur with a 302 redirect, which is used for temporary redirections. In the case of a 302 redirect, the original pages remain indexed as the redirection is understood to be temporary, indicating that the original content or page is expected to return at the old URL in the future. Understanding this distinction is crucial for SEO strategies, as it directly affects how search engines index and rank web pages.

Imran Khan

Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.

  • Languages: Java, Python
  • Frameworks: J2EE, Spring, Struts 2.0, Hibernate
  • Web Technologies: React, HTML, CSS
  • Analytics: Adobe Analytics
  • Tools & Technologies: IntelliJ, JIRA

🌐 LinkedIn

📝 Blogs

📧 Imran Khan