Expand my Community achievements bar.

Dynamic vanity URLs in AEM

Avatar

Level 4

How can we create Vanity URLs based on a query parameter to a page?

We have a URL: http://www.myhost.com/my-page?name=<firstname>.<lastname>.
We want to pretty it up to look like this: http://www.myhost.com/my-page/<firstname>.<lastname>.
eg.
http://www.myhost.com/my-page?name=john.smith
should show as http://www.myhost.com/my-page/john.smith

Am I correct in saying this is not as simple as creating a rewrite rule on apache? Possibly an AEM servlet? A servlet that uses a request filter to fetch the query parameter and form the dynamic URL and 301 redirect to new URL. I was also trying to do these with Resource resolver mappings (etc/map). Could it be done just with Resource resolver mappings (/etc/maps)?

2 Replies

Avatar

Level 4

You'll need to use a sling servlet with a selector. Your selectors will be firstname, lastname. Your sling servlet annotation will look like this:
@SlingServlet(resourceTypes = "myBrand/components/pages/myPeoplePageType", selectors =

{firstname, lastname}

, extensions = "html”, methods=”GET”)

Your URL below:
http://www.myhost.com/people.firstname.lastname
will invoke your servlet and pass current page path along with other parameters. Servlet would be resolved through selectors & resource type.

Then you will need a dispatcher rewrite rule that will take /my-page/john.smith and transform it into /my-page.john.smith. You can test your /my-page.john.smith on your author instance. But for the pretty URL, you'll need your dispatcher set up. Or you might be able to do this URL mapping (under /etc/maps) as a regexmap (https://sling.apache.org/documentation/the-sling-engine/mappings-for-resource-resolution.html#mappin...), in which case, you can test it in your author.

https://forums.adobe.com/thread/2325530
http://www.aemcq5tutorials.com/tutorials/sling-servlet-in-aem/
https://helpx.adobe.com/experience-manager/6-2/managing/using/seo-and-url-management.html (Look at sections - "Using Sling Selectors" and "Sling servlets (one level down)")