AEM/Sling动态选择器实现:SEO友好姓名参数URL配置
Got it, let's walk through how to build this SEO-friendly URL setup in AEM— I’ve tackled similar requirements before, so here’s a practical step-by-step guide:
First, you need to map the clean URL pattern to your actual AEM page while extracting the firstname and lastname parameters. This happens via AEM's Resource Resolver configuration:
- Go to AEM Console > OSGi Configurations (usually at
/system/console/configMgr). - Find and edit the Apache Sling Resource Resolver Factory configuration.
- In the Resource Resolver Mappings field, add this regex-based mapping (adjust the target path to match your actual page location):
/mycontent\.(.*)\.(.*)\.html=/content/your-site/mycontent.html?firstname=$1&lastname=$2- The regex
\.(.*)\.(.*)\.htmlcaptures the first and last name from the URL. - The
$1and$2placeholders pass these values as query parameters to your actual page.
- The regex
- Save the configuration— AEM will apply the mapping immediately (no restart needed for most cases).
Next, create a component (or modify your existing mycontent page component) to fetch the parameters and render the user's info. Using Sling Models is the cleanest approach here:
Create a Sling Model
Write a Java class to handle parameter extraction and formatting:
package com.yourproject.models; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.injectorspecific.SlingObject; @Model(adaptables = SlingHttpServletRequest.class) public class UserProfileModel { @SlingObject private SlingHttpServletRequest request; public String getFormattedFirstName() { String firstName = request.getParameter("firstname"); return formatName(firstName); } public String getFormattedLastName() { String lastName = request.getParameter("lastname"); return formatName(lastName); } private String formatName(String name) { if (name == null || name.isEmpty()) { return ""; } return name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase(); } }
Update the HTL Template
Add this to your component's HTL file to use the model and display the user's name:
<sly data-sly-use.profile="com.yourproject.models.UserProfileModel"> <div class="user-profile"> <h1>Hello, ${profile.formattedFirstName} ${profile.formattedLastName}!</h1> <!-- Add your user-specific content here (e.g., profile details, dynamic content) --> </div> </sly>
Deploy your code and configuration, then test the URLs:
- Visit
www.host.com/mycontent.richard.williams.html— you should see "Hello, Richard Williams!" - Visit
www.host.com/mycontent.john.smith.html— you should see "Hello, John Smith!"
To make this robust, add logic in your Sling Model to check if the user exists (e.g., query AEM for a user node or external service). If the user isn't found, trigger a 404:
// Add this to your Sling Model (inject SlingHttpServletResponse) @SlingObject private SlingHttpServletResponse response; public void init() throws IOException { String firstName = request.getParameter("firstname"); String lastName = request.getParameter("lastname"); if (!userExists(firstName, lastName)) { response.sendError(HttpServletResponse.SC_NOT_FOUND); } } private boolean userExists(String firstName, String lastName) { // Add your logic here to check if the user exists return true; // Replace with actual check }
- Mapping Order: Ensure your regex mapping comes before any generic mappings (e.g.,
/content/your-site(/.*)$=$1) to avoid conflicts. - Caching: If user content is dynamic, configure proper caching headers or use AEM's dispatcher rules to prevent caching of personalized content.
- URL Validation: Add server-side validation to sanitize the
firstnameandlastnameparameters to avoid security issues.
内容的提问来源于stack exchange,提问作者mrjayviper

