How to stop WordPress from removing & from URL?

So I have a URL which has a tracking link applied to the end. For some reason WordPress is removing the which is causing the tracking to fail. You can see the URL example here:

http://www.domain.co.uk/internalpage/?mkwid=smWfvaLGf_dm

When you go through this, you then get redirected in a way to:

http://www.domain.co.uk/internalpage/?mkwid=smWfvaLGf_dm

Note the has been removed. Also, this only happens on internal pages. Does anyone know how to stop this?

Topic urls permalinks bug-tracking Wordpress

Category Web


Here's the why part:

This part of the redirect_canonical() is removing the leading & in the redirect query part:

// tack on any additional query vars
$redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );

Example:

example.tld/?&a=1&b=2&c=3 

is redirected to

example.tld/?a=1&b=2&c=3 

If you must have the leading & you might try to adjust it through the redirect_canonical filter:

/**
 * Filter the canonical redirect URL.
 *
 * Returning false to this filter will cancel the redirect.
 *
 * @since 2.3.0
 *
 * @param string $redirect_url  The redirect URL.
 * @param string $requested_url The requested URL.
 */
$redirect_url = apply_filters( 'redirect_canonical', $redirect_url, $requested_url );

You can use here rawurlencode() function. First pass "?&mkwid=smWfvaLGf_dm" this url into the function then you will get "%3F%26mkwid%3DsmWfvaLGf_dm" encoded value.

Now go on the functions.php and put this code there (But this code will run on every call of you site)

//for accessing current url
$accessUrl = get_site_url() .$_SERVER['REQUEST_URI'];

$pos = strpos($accessUrl, "%");
if ($pos !== false) 
{
    $accessString = rawurldecode($accessUrl);
    wp_redirect($accessString);
    exit();
}

I feel this will help you. Thanks

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.