You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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:

Step 1: Set Up Resource Resolver Mappings

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 \.(.*)\.(.*)\.html captures the first and last name from the URL.
    • The $1 and $2 placeholders pass these values as query parameters to your actual page.
  • Save the configuration— AEM will apply the mapping immediately (no restart needed for most cases).
Step 2: Build a Component to Display User-Specific Content

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>
Step 3: Test the Setup

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!"
Optional: Handle Missing/Invalid Users

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
}
Key Notes
  • 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 firstname and lastname parameters to avoid security issues.

内容的提问来源于stack exchange,提问作者mrjayviper

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:06:08