REST Absolute URLs¶
Replaces relative URLs with absolute URLs in web services.
Table of Contents¶
Introduction¶
REST Absolute URLs replaces relative file / image URLs with absolute URLs in content fields when they are exposed through REST web services such as JSON:API.
This module addresses issues in headless/decoupled Drupal implementations where frontend frameworks cannot access images and files with relative URLs.
Functionality¶
- Converts relative internal URLs to absolute URLs
- Processes content fields exposed through REST services
- Compatible with Drupal 11.3 or later, including 12
- URL processing occurs during serialization only
Requirements¶
This module requires the following:
- Core Serialization module: Must be enabled
- REST or JSON:API: Required for exposing content to external applications
Compatible Modules¶
- JSON:API - Core module for RESTful web services
- RESTful Web Services - Alternative REST implementation
Installation¶
Install as you would normally install a contributed Drupal module. For further information, see Installing Drupal Modules.
Configuration¶
Basic Setup¶
No additional configuration is required after installation. The module works automatically once enabled.
Configuration¶
When using containerized environments like Docker, or when server-side rendering
with Node.js, Drupal may use internal container names instead of the actual
domain. To resolve this, explicitly set the base URL in your settings.php:
Multiple Environment Configuration¶
For different environments, you can conditionally set the base URL:
// In settings.php or settings.local.php
if (getenv('ENVIRONMENT') === 'production') {
$config['rest_absolute_urls']['base_url'] = 'https://production.example.com';
} elseif (getenv('ENVIRONMENT') === 'staging') {
$config['rest_absolute_urls']['base_url'] = 'https://staging.example.com';
}
Usage¶
Basic Usage Example¶
You add an image to a body field using Ckeditor: you can see this image in Drupal, but when you fetch this node with any frontend framework (like React.js, Angular, etc), then this image is not visible. That's because the image's URL is relative, and can't be found.
This module automatically replaces such relative internal URLs with absolute URLs, so they point to the right server with Drupal where the files are really located.
Before (relative URL):
After (absolute URL with this module):
{
"body": {
"value": "<img src=\"https://example.com/sites/default/files/image.jpg\"
alt=\"Example\">"
}
}
Content Types Affected¶
The module processes all content fields including:
- Body fields with embedded images
- Text fields with internal links
- Any field containing HTML with relative URLs
Views REST Export¶
Both row plugins of a Views REST Export display are covered:
- Entity (
data_entity): passes the full entity through the normalizer chain; every text field'svalueandprocessedoutput is rewritten. - Fields (
data_field): each field is rendered to aMarkupInterfacevalue via the Views field pipeline. The module'sMarkupNormalizerrewrites such markup values before they reach the encoder, wherever the serializer receives them.
Edge case — Fields row with "Raw output" enabled: when a field option sets Raw output the row plugin returns the field's raw scalar value directly, bypassing the render pipeline and therefore any normalizer. URLs in raw values are not rewritten. Turn Raw output off, or switch to the Entity row plugin, if URL rewriting is required for that field.
Troubleshooting¶
Common Issues¶
Images still showing relative URLs
- Ensure the Serialization module is enabled
- Clear all caches: drush cr
- Verify the content is being accessed through REST/JSON:API
Wrong base URL in Docker environments - Set the explicit base URL configuration as shown above - Ensure your domain configuration is correct in Docker
Module not working with custom content - The module only affects content exposed through REST services - Standard Drupal page rendering is not affected