Expand my Community achievements bar.

Applications for the 2024-2025 Adobe Experience Manager Champion Program are open!
SOLVED

Reverse Mapping fragment indentifier URL in etc/map

Avatar

Level 1

I would like to know if it is possible to reverse map fragment identifier URL using sling:match as it is not shortening the URL only for those containing #

sling:internalRedirect :/content/website/(.*)

sling:match:/$1

sling:internalRedirect:/content/website/(.*)#signup
sling:match:/$1#signup 


Both properties are unable to shorten URL

1 Accepted Solution

Avatar

Correct answer by
Level 7

Hi @Zendarkke ,

In Apache Sling, when defining mappings in the etc/map configuration, it's important to understand that the fragment identifier (the part of a URL that starts with #) is typically not considered part of the path. Therefore, when using sling:match to define mappings, it won't directly match the fragment identifier.

However, you can use Sling rewrite rules to achieve a similar effect. Here's how you can approach it:

  1. Define a Rewrite Rule: Create a rewrite rule that captures the fragment identifier and appends it as a request parameter.

  2. Update the Mapping: Adjust the mapping to consider the appended request parameter.

Here's an example:

Step 1: Define Rewrite Rule

You can define a rewrite rule to capture the fragment identifier and append it as a request parameter. Add this rule in the Apache Sling Rewrite Rules configuration (/apps/yourapp/config/org.apache.sling.rewriter.impl.RewriterImpl) or a similar location:

# Rewrite rule to capture fragment identifier and append as request parameter
RewriteRule ^/content/website/(.*)#(.*)$ /content/website/$1?fragment=$2 [NE,L,QSA]

Step 2: Update Mapping

Now, update your mapping in the etc/map configuration to consider the appended fragment parameter. For example:

# Mapping to match URLs with appended fragment parameter
/some/short/url
sling:internalRedirect:/content/website/(.*)?fragment=#signup
sling:match:$1

With this setup, when a URL like /some/short/url is accessed, the rewrite rule captures the fragment identifier (if any) and appends it as a request parameter (fragment). Then, the mapping matches the URL with the appended fragment parameter, allowing you to perform the desired internal redirect based on the original URL with the fragment identifier.

This approach effectively achieves a reverse mapping of fragment identifier URLs in Apache Sling.

View solution in original post

2 Replies

Avatar

Community Advisor

Hi @Zendarkke 

The `sling:internalRedirect` and `sling:match` properties in Apache Sling are used for URL mapping and redirection. However, they do not support fragment identifiers (the part of the URL after the `#` symbol) in the mapping or matching process.

 

The `sling:internalRedirect` property is used to specify the target URL to which the request should be internally redirected. The `sling:match` property is used to define the pattern that the request URL should match in order to trigger the internal redirect.

 

In your example, the `sling:internalRedirect` property `/content/website/(.*)#signup` will not work as expected because the fragment identifier `#signup` is not considered during the matching process.

 

If you need to handle fragment identifiers in your URL mapping, you may need to consider using a different approach or customizing the URL mapping logic in your application. One possible solution could be to handle the fragment identifier on the client-side using JavaScript or by implementing a custom Sling servlet or filter to handle the redirection based on the fragment identifier.



Avatar

Correct answer by
Level 7

Hi @Zendarkke ,

In Apache Sling, when defining mappings in the etc/map configuration, it's important to understand that the fragment identifier (the part of a URL that starts with #) is typically not considered part of the path. Therefore, when using sling:match to define mappings, it won't directly match the fragment identifier.

However, you can use Sling rewrite rules to achieve a similar effect. Here's how you can approach it:

  1. Define a Rewrite Rule: Create a rewrite rule that captures the fragment identifier and appends it as a request parameter.

  2. Update the Mapping: Adjust the mapping to consider the appended request parameter.

Here's an example:

Step 1: Define Rewrite Rule

You can define a rewrite rule to capture the fragment identifier and append it as a request parameter. Add this rule in the Apache Sling Rewrite Rules configuration (/apps/yourapp/config/org.apache.sling.rewriter.impl.RewriterImpl) or a similar location:

# Rewrite rule to capture fragment identifier and append as request parameter
RewriteRule ^/content/website/(.*)#(.*)$ /content/website/$1?fragment=$2 [NE,L,QSA]

Step 2: Update Mapping

Now, update your mapping in the etc/map configuration to consider the appended fragment parameter. For example:

# Mapping to match URLs with appended fragment parameter
/some/short/url
sling:internalRedirect:/content/website/(.*)?fragment=#signup
sling:match:$1

With this setup, when a URL like /some/short/url is accessed, the rewrite rule captures the fragment identifier (if any) and appends it as a request parameter (fragment). Then, the mapping matches the URL with the appended fragment parameter, allowing you to perform the desired internal redirect based on the original URL with the fragment identifier.

This approach effectively achieves a reverse mapping of fragment identifier URLs in Apache Sling.