I am using a Function for redirection of external links in my website
How This Function works:- it will detect all nearbuy.com links and Apply a redirection Rule and then redirected.
For Example:- nearbuy.com is a domain So this function add prefix before nearbuy.com external url i.e http://tracking.nearbuy.com/aff_c?offer_id=9&aff_id=479&url=
Then http://www.nearbuy.com and url parameter to make it work.
What i want to do:-
1) If nearbuy.com link already has "?" then it will use "&"
2) If nearbuy.com link don't have "?" then it will use "?"
What is problem:-
I am using below Method but its not detecting "?" in links and add "?" again in end of links while redirection which is problem
Here is Code:-
if(stripos($url, "nearbuy.com")) {
$url_args = parse_url($url);
$url_params = "?utm_source=nap&utm_medium=cps&utm_campaign={affiliate_id}";
if(!empty($durl["query"])) {
$url_params = "&utm_source=nap&utm_medium=cps&utm_campaign={affiliate_id}";
}
$redirect_url = "http://tracking.nearbuy.com/aff_c?offer_id=9&aff_id=479&url=" . urlencode($url . $url_params);
goto redirect_to_uri;
break;
}
You could use parse_url
and do something like:
$yourQueryParams = [
'offer_id' => 9,
'aff_id' => 479,
];
$url .= (parse_url($url, PHP_URL_QUERY) ? '&' : '?') . http_build_query($yourQueryParams);
// redirect with your query params
header("Location: $url");
die();