AEM default redirect template adding .html in publish | Community
Skip to main content
vinodp18961354
December 9, 2019
Solved

AEM default redirect template adding .html in publish

  • December 9, 2019
  • 2 replies
  • 3037 views

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!!

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Nirmal_Jose

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)?"
      ]
    }

}

2 replies

Nirmal_Jose
Adobe Employee
Nirmal_JoseAdobe EmployeeAccepted solution
Adobe Employee
December 15, 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)?"
      ]
    }

}

December 16, 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;
}