Sometimes we require to redirect request to old or deprecated pages in our sites to new versions of that page or to a totaly different site. For instance, lets suppose that we decided to redirect the page http://my.site.com/myoldpage to http://my.new.site/mynewpage. In that case, because the new page is in a different domain, we need an external redirect which is basically a Sling mapping.
Understanding sling mappings
The sling mappings expose properties that allow us to modify the way a resource is resolved. Said properties are:
- sling:match. Defines a partial regular expresion which be used to macth the incoming request instead of the resource name.
- sling:redirect. The value of this property is sent back in the Location header in the response, which causes a redirection.
- sling:status. The value of this property is sent back as the HTTP status to the client when we use the sling:redirect property. It defaults to 302, but you can use 300 (Multiple Choices), 301 (Moved Permanently), 303 (See Other), and 307 (Temporary Redirect).
- sling:internalRedirect. Multi-value property. It modifies the path internally so the path specifed in sling:match can resolve to a given resource.
- sling:alias. Adds an alias for a page, e.g
Creating an external redirect
First we should create the mapping under the /etc/map/http node. We have two options to create the node, we could use the namespace as the name e.g. “localhost.4502” or we can use another custom name e.g. “localhost_any” in conjunction with the “sling:match” property. For this example, we will use the first approach.
- Create a folder under /etc/map/http with the name “localhost.4502”.
- Create a node inside that folder with any name you want e.g. “togoogle”, the type will be “sling:Mapping”
- Add the following properties:
- “sling:match” with the URL you want to be redirected, e.g. “content/oldcontent.html”
- “sling:internalRedirect” with the URL of the page you want to redirect to, e.g. “https://google.com”
- “sling:status” choose any of the suported statuses, in thi case we will choose 301
What if the page I want to redirect to is in the same server and domain?
In that case it’s not necessary to create an external redirect. Instead you can:
- Use a vanity url
- Specify and alias with a Sling mapping (sling:alias) so
content/myoldpagecan be considered an alias of
- Use an internal redirect to transparently redirect
content/myoldpage. Please check my previous post and its second part for more information about internal redirects.
Well, that was it for today! thanks for reading.