Describe the bug
I have setup some redirections for having utm parameters hidden behind shorter links, like
https://example.com/partner-instagram redircts to https://example.com/?utm_medium=button&utm_source=instagram&utm_content=profil&utm_campaign=website-buttons.
Sadly some services like instagram will add some additional parameter to the url like this: https://example.com/partner-instagram?fbclid=PAZXh0bgNhZW0CMTEAAaf1-7aQ0LrYbkOoNOJqMK0VHOwSswZPCCNnVioFMNUGKTuE5I4mVnnaCIspVA_aem_0t4QVJk4xOGV0CX1pHasuA&fbclid=PAQ0xDSwLtljVleHRuA2FlbQIxMAABp3xqN3fubgEhDnTGlo5cOomSQD19oHgCisK9oNwS13UfbXL-zciGB9ryhP--_aem_rH2C-3SOrKao--xBK4GJcQ which then redircts to https://example.com/?utm_medium=button&utm_source=instagram&utm_content=profil&utm_campaign=website-buttons?fbclid=PAZXh0bgNhZW0CMTEAAaf1-7aQ0LrYbkOoNOJqMK0VHOwSswZPCCNnVioFMNUGKTuE5I4mVnnaCIspVA_aem_0t4QVJk4xOGV0CX1pHasuA&fbclid=PAQ0xDSwLtljVleHRuA2FlbQIxMAABp3xqN3fubgEhDnTGlo5cOomSQD19oHgCisK9oNwS13UfbXL-zciGB9ryhP--_aem_rH2C-3SOrKao--xBK4GJcQ
Which is a broken query parameter. We'd just need to merge the query parameters here. And its quite simple, the referring code is located here:
|
$this->redirect_to .= '?' . $this->query_string; |
To Reproduce
Steps to reproduce the behavior:
- Add a redirect as mentioned above
- Open the modified link as mentioned above
- Check the not correct query parameter "merge" (no real merge happens, that is the problem)
Expected behavior
I would expect to merge the query parameters chaning the second ? to & in the code:
https://example.com/?utm_medium=button&utm_source=instagram&utm_content=profil&utm_campaign=website-buttons&fbclid=PAZXh0bgNhZW0CMTEAAaf1-7aQ0LrYbkOoNOJqMK0VHOwSswZPCCNnVioFMNUGKTuE5I4mVnnaCIspVA_aem_0t4QVJk4xOGV0CX1pHasuA&fbclid=PAQ0xDSwLtljVleHRuA2FlbQIxMAABp3xqN3fubgEhDnTGlo5cOomSQD19oHgCisK9oNwS13UfbXL-zciGB9ryhP--_aem_rH2C-3SOrKao--xBK4GJcQ
Additional context
Original code:
|
$this->redirect_to .= '?' . $this->query_string; |
if ( true === $this->do_filter( 'redirection/add_query_string', true ) && Str::is_non_empty( $this->query_string ) ) {
$this->redirect_to .= '?' . $this->query_string;
}
Some code solution suggested by the ai (not tested):
if ( true === $this->do_filter( 'redirection/add_query_string', true ) && Str::is_non_empty( $this->query_string ) ) {
if ( strpos( $this->redirect_to, '?' ) !== false ) {
$this->redirect_to .= '&' . $this->query_string;
} else {
$this->redirect_to .= '?' . $this->query_string;
}
}
And some more detailed code, which also supports fragments like #anchor. You might need to take a closer look, if that is correct (not tested)
if ( true === $this->do_filter( 'redirection/add_query_string', true ) && Str::is_non_empty( $this->query_string ) ) {
// Parse the existing redirect URL
$parsed_url = parse_url( $this->redirect_to );
// Extract existing query parameters from the redirect URL (if any)
$target_params = [];
if ( isset( $parsed_url['query'] ) ) {
parse_str( $parsed_url['query'], $target_params );
}
// Parse the incoming query string (e.g. from the original request, like fbclid)
$new_params = [];
parse_str( $this->query_string, $new_params );
// Merge both sets of parameters (new ones can override existing ones)
$merged_params = array_merge( $target_params, $new_params );
// Rebuild the full query string
$merged_query = http_build_query( $merged_params );
// Rebuild the final redirect URL with merged parameters
$this->redirect_to =
( isset( $parsed_url['scheme'] ) ? $parsed_url['scheme'] . '://' : '' ) .
( $parsed_url['host'] ?? '' ) .
( $parsed_url['path'] ?? '' ) .
'?' . $merged_query .
( isset( $parsed_url['fragment'] ) ? '#' . $parsed_url['fragment'] : '' );
}
Describe the bug
I have setup some redirections for having utm parameters hidden behind shorter links, like
https://example.com/partner-instagramredircts tohttps://example.com/?utm_medium=button&utm_source=instagram&utm_content=profil&utm_campaign=website-buttons.Sadly some services like instagram will add some additional parameter to the url like this:
https://example.com/partner-instagram?fbclid=PAZXh0bgNhZW0CMTEAAaf1-7aQ0LrYbkOoNOJqMK0VHOwSswZPCCNnVioFMNUGKTuE5I4mVnnaCIspVA_aem_0t4QVJk4xOGV0CX1pHasuA&fbclid=PAQ0xDSwLtljVleHRuA2FlbQIxMAABp3xqN3fubgEhDnTGlo5cOomSQD19oHgCisK9oNwS13UfbXL-zciGB9ryhP--_aem_rH2C-3SOrKao--xBK4GJcQwhich then redircts tohttps://example.com/?utm_medium=button&utm_source=instagram&utm_content=profil&utm_campaign=website-buttons?fbclid=PAZXh0bgNhZW0CMTEAAaf1-7aQ0LrYbkOoNOJqMK0VHOwSswZPCCNnVioFMNUGKTuE5I4mVnnaCIspVA_aem_0t4QVJk4xOGV0CX1pHasuA&fbclid=PAQ0xDSwLtljVleHRuA2FlbQIxMAABp3xqN3fubgEhDnTGlo5cOomSQD19oHgCisK9oNwS13UfbXL-zciGB9ryhP--_aem_rH2C-3SOrKao--xBK4GJcQWhich is a broken query parameter. We'd just need to merge the query parameters here. And its quite simple, the referring code is located here:
seo-by-rank-math/includes/modules/redirections/class-redirector.php
Line 139 in 9a198ad
To Reproduce
Steps to reproduce the behavior:
Expected behavior
I would expect to merge the query parameters chaning the second
?to&in the code:https://example.com/?utm_medium=button&utm_source=instagram&utm_content=profil&utm_campaign=website-buttons&fbclid=PAZXh0bgNhZW0CMTEAAaf1-7aQ0LrYbkOoNOJqMK0VHOwSswZPCCNnVioFMNUGKTuE5I4mVnnaCIspVA_aem_0t4QVJk4xOGV0CX1pHasuA&fbclid=PAQ0xDSwLtljVleHRuA2FlbQIxMAABp3xqN3fubgEhDnTGlo5cOomSQD19oHgCisK9oNwS13UfbXL-zciGB9ryhP--_aem_rH2C-3SOrKao--xBK4GJcQAdditional context
Original code:
seo-by-rank-math/includes/modules/redirections/class-redirector.php
Line 139 in 9a198ad
Some code solution suggested by the ai (not tested):
And some more detailed code, which also supports fragments like
#anchor. You might need to take a closer look, if that is correct (not tested)