Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

AEM default redirect template adding .html in publish

Avatar

Level 1

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

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

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

}

View solution in original post

2 Replies

Avatar

Correct answer by
Employee Advisor

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

}

Avatar

Level 1

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;
}