This module extends the Redirect module to support regex pattern matching in addition to exact path matching using the redirect entities.
Add to composer.json
:
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/baikho/drupal-redirect_regex"
}
],
"require": {
"drupal/redirect_regex": "1.0.0-alpha5"
}
}
And run:
composer require drupal/redirect_regex:1.0.0-alpha5
This module follows the same architectural patterns as the redirect module:
- Extends RedirectRepository: The
RedirectRegexRepository
extends theRedirectRepository
class - Service Override: Uses a service provider to override the
redirect.repository
service - Convention-Based: Regex redirects are identified by source paths starting with
regex:
- Backward Compatibility: All existing redirect functionality continues to work unchanged
- Service Override: The module overrides the
redirect.repository
service to useRedirectRegexRepository
- Fallback Logic:
RedirectRegexRepository::findMatchingRedirect()
first checks for regular redirects, then falls back to regex redirects - Efficient Querying: Only regex redirects (those with
regex:
prefix) are loaded from the database, not all redirects - Multilingual Support: Patterns are tested against paths both with and without language prefixes
- Regex Convention: Regex redirects are stored as normal redirect entities but with source paths prefixed with
regex:
- Pattern Extraction: The actual regex pattern is extracted by removing the
regex:
prefix
To create a regex redirect:
- Go to
/admin/config/search/redirect/add
- Set the From field to:
regex:your-pattern
- Example:
regex:user\/\d+\/profile
- Example:
- Set the To field to the redirect target
- Save the redirect
Important Notes:
- Do NOT include a leading slash in the regex pattern (e.g., use
user\/\d+\/profile
, not\/user\/\d+\/profile
) - The redirect system automatically strips leading slashes before matching
- Remember to escape backslashes in the admin interface (type
\/
for literal/
in regex) - For redirects from existing routes, enable "Allow redirects from aliases" in redirect settings
- Multilingual Support: Patterns are tested against both the current path and the path with language prefix (e.g.,
user/123/profile
anden/user/123/profile
)
This module seamlessly integrates with:
- GraphQL: Route queries automatically check both redirect types
- HTTP Redirects: Request subscribers use the unified repository
- Admin Interface: Uses existing redirect admin pages
- API: Any code using
redirect.repository
gets regex support automatically
redirect:redirect
(>=1.12) - provides the redirect entities and admin interface
- Enable the module:
drush en redirect_regex
- Create regex redirects using the standard redirect admin interface with the
regex:
prefix - All existing redirect functionality continues to work
- GraphQL route queries now support regex redirects automatically
Source Path | Redirect Target | Description |
---|---|---|
regex:blog\/\d+\/.* |
/blog/archive |
Redirect old blog URLs to archive |
regex:user\/\d+\/profile |
/user/profile |
Redirect user profile URLs |
regex:page\/old\/([0-9a-z]+) |
/page/new-page |
Redirect old page URLs with alphanumeric IDs |