How to
Handle canonical URLs
Learn how to handle canonical URLs and redirects in Geins Merchant API and Management API.
Prerequisites
- Basic understanding of URLs and slugs in Geins
- Access to Merchant API
- Familiarity with your platform's URL structure
Goal
- Ensure users are directed to the canonical URL for products
- Implement redirects for outdated or changed URLs
- Maintain SEO integrity by using canonical URLs
Architecture at a glance
- Use the Merchant API to fetch entities by slug → Check for canonical URL → Redirect if necessary
APIs used
- Merchant API:
https://merchantapi.geins.io/graphql
Step-by-step
Fetch product by slug using Merchant API
When fetching an entity (e.g., product, category) using the Merchant API, the system automatically checks the slug history to find the current slug if an old slug is used. This ensures that you always get the canonical URL for the entity. This is done for all segments in the URL, resulting in a canonicalUrl being returned in the response, even if the current slug does not differ from the requested slug.
Request example
query product(
$alias: String!
$channelId: String
$languageId: String
$marketId: String
) {
product(
alias: $alias
channelId: $channelId
languageId: $languageId
marketId: $marketId
) {
productId
articleNumber
canonicalUrl
name
}
}
{
"Accept": "application/json",
"X-ApiKey": "{MERCHANT_API_KEY}",
}
{
"alias": "{PRODUCT_ALIAS}",
"channelId": "{CHANNEL_ID}",
"languageId": "{LANGUAGE_ID}",
"marketId": "{MARKET_ID}"
}
curl -X POST https://merchantapi.geins.io/graphql \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-H "X-ApiKey: {MERCHANT_API_KEY}" \
-d '{"query":"query product($alias: String!, $channelId: String, $languageId: String, $marketId: String) { product(alias: $alias, channelId: $channelId, languageId: $languageId, marketId: $marketId) { productId articleNumber canonicalUrl name } }","variables":{"alias":"{PRODUCT_ALIAS}","channelId":"{CHANNEL_ID}","languageId":"{LANGUAGE_ID}","marketId":"{MARKET_ID}"}}'
The
channelId, languageId, and marketId arguments are optional and can be left out to use default values.Response example
200 OKresponse.json
{
"data": {
"product": {
"productId": "12345",
"articleNumber": "SKU123",
"canonicalUrl": "/market/language/p/category/new-product-slug",
"name": "New Product Name"
}
}
}
Handle canonical URL in your application
If the canonicalUrl in the response differs from the requested URL, you can choose to redirect the user to the canonical URL.
Related