diffa
diffa

Reputation: 2996

Can Sling mappings be restricted to requests with host header

I would like to selectively apply Sling mappings defined in sling:Mapping nodes under /etc/map.publish and can't get the behaviour I would like.

Essentially, I would like the mapping rule to trigger only when the host header matches the request.

I am currently using sling:Mapping nodes under /etc/map.publish to map resource paths to short URLs in the response.

So under /etc/map.publish/http/myapp I would have the following node:

<jcr:root ...>
    jcr:primaryType="sling:Mapping"
    sling:internalRedirect="/content/company/app/en"
    sling:match="app.company.com
</jcr:root>

What I would like is that when a user requests:

http://app.company.com/content/company/app/en/page.html

The urls in the response (when mapped) will return in the form:

http://app.company.com/page.html

The reason for this difference in inbound and outbound urls is because I have Apache rewriting URLs for different device types.

However, when a request with a different host header arrives, such as:

http://localhost:4502/content/company/app/en/page.html

I do not want the URLs to be mapped according to that rule. Right now, it is being mapped to

http://app.company.com/page.html

It seems as though the mapping is strictly resolves the resource using considering the host/port. Then when mapping urls during output a "best match" is found and used. I would like the map() to behave like the resolve() if possible.

Upvotes: 3

Views: 2940

Answers (2)

cwoeltge
cwoeltge

Reputation: 201

If you want to make sure your inbound request is resolved, just add a second mapping to it.

Your mapping would look like this:

<jcr:root ...>
    jcr:primaryType="sling:Mapping"
    sling:internalRedirect="[/content/company/app/en,/content,/]"
    sling:match="app.company.com
</jcr:root>

Outbound mappings, s.a. resolver.map(), will use the first applying rule.

Upvotes: 1

Tomek Rękawek
Tomek Rękawek

Reputation: 9304

There are two mechanisms based on /etc/map:

  1. URL resolver using resolver.resolve() responsible for transforming URLs like http://app.company.com/page.html into content path, eg. /content/company/app/en/page.html
  2. Link rewriter using resolver.map() method which transforms the content and shortens all links from /content/company/app/en/page.html form in <a>, <img>, etc. to full URL. It will work only if you don't have any regular expressions in apropriate sling:match property.

You can use domain name to map/resolve content and eg. create multidomain environment, so http://app.company.com/page.html will hit one resource and http://app.company2.com/page.html will hit another.

However, you can't disable or enable link rewriter depending on the current request host. Eg. if configure mappings as above, the /content/company/app/en/page.html content path will always be shortened to http://app.company.com/page.html, no matter what host header you have in your request.

Upvotes: 1

Related Questions