AEM default redirect template adding .html in publish

Avatar

Avatar

vinodp18961354

Avatar

vinodp18961354

vinodp18961354

09-12-2019

Hi All,

As part of beautification of URL we have removed .html from publish in page i.e. URL would be www.test.com/publish. We are using AEM inbuilt 302 redirection where it is explicitly adding .html to redirected URL.

please help if there is anyway to remove .html addition from default AEM redirect template.

Thanks in advance!!

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

Nirmal_Jose

MVP

Avatar

Nirmal_Jose

MVP

Nirmal_Jose
MVP

15-12-2019

We can different approaches to get a polished URL

 

1. Create rewrite rules in web server level so that any requests resolves to full content path on to AEM publisher

 

RewriteRule ^/$ /content/org/test/en.html [PT]

 

RewriteCond %{REQUEST_URI} ^/test$
RewriteRule ^/test$ /content/org/test/en.html [PT,L]

 

RewriteCond %{REQUEST_URI} ^/test/$
RewriteRule ^/test/$ /content/org/test/en.html [PT,L]

 

RewriteCond %{REQUEST_URI} ^/test/(.+)\.html$
RewriteRule ^/test/(.*)/$ /content/org/test/en/$1.html [PT,L]

 

RewriteCond %{REQUEST_URI} !^/test/(.+)\.html$
RewriteCond %{REQUEST_URI} ^/test/(.+)/$
RewriteRule ^/test/(.*)/$ /content/org/test/en/$1.html [PT,L]

 

RewriteCond %{REQUEST_URI} !^/test/(.+)\.html$
RewriteCond %{REQUEST_URI} !^/test/(.+)/$
RewriteCond %{REQUEST_URI} ^/test/(.+)$
RewriteRule ^/test/(.*)$ /content/bt/test/en/$1.html [PT,L]

 

The above rules will ensure all types of request, no HTML, trailing slash, with HTML are served.

 

2. Create forward and reverse sling mappings 

Forward

{

  • jcr:primaryType"sling:Mapping",
  • sling:match"www.domain.com/$",
  • sling:internalRedirect: [
    • "/content/org/test/en.html"
    ]

}

 

Reverse

{

  • jcr:primaryType"sling:Mapping",
  • reverse_mapping_content: {
    • jcr:primaryType"sling:Mapping",
    • sling:match"$1",
    • sling:internalRedirect: [
      • "/content/org/test/en/(.*).html"
      ]
    },
  • reverse_mapping_content_nohtml: {
    • jcr:primaryType"sling:Mapping",
    • sling:match"$1",
    • sling:internalRedirect: [
      • "/content/org/test/en/(.*)"
      ]
    },
  • reverse_mapping_root: {
    • jcr:primaryType"sling:Mapping",
    • sling:match"$",
    • sling:internalRedirect: [
      • "/content/org/test/en(.html)?"
      ]
    }

}

Answers (1)

Answers (1)

Avatar

Avatar

alessandrod5385

Avatar

alessandrod5385

alessandrod5385

16-12-2019

maybe another way is to sendRedirect() the response could be from the java model, this method redirects to first child: 

response.sendRedirect(Util.getModifiedLink(currentPage.listChildren().next().getPath()));

getModifiedLink() test if link is internal or external and adds .html only if needed:

public static String getModifiedLink(String link) {
if (StringUtils.isNotEmpty(link) && !link.equals("#") && !(link.startsWith("/") && link.contains(".pdf")) && !link.contains("@")) {
link = link.startsWith("/") ? link + ".html" : link.contains("https://") ?
link : link.contains("http://") ? link : "http://" + link;
}
return link;
}