从URL/主机中删除子域以匹配附属链接数组中的域



我想使用php制作一个重定向文件,它可以自动将Affiliates标记添加到所有链接中。就像它的工作原理https://freekaamaal.com/links?url=https://www.amazon.in/。

如果我打开上面的链接,它会自动将附属标签添加到链接中,最后打开的链接是这样的https://www.amazon.in/?tag=freekaamaal-21'Flipkart和许多其他网站也是如此。它会自动将附属标签添加到各种链接中。例如amazon、Flipkart、ajio等。

如果有人能在这方面帮助我,我将不胜感激。

提前感谢

现在我在下面做了这个代码,但问题是有时链接有额外的子域,例如https://dl.flipkart.com/或https://m.shopclues.com/,等等。对于这些类型的链接,它不会从数组重定向,而是重定向到默认链接。

<?php
$subid = isset($_GET['subid']) ? $_GET['subid'] : 'telegram'; //subid for external tracking
$affid = $_GET['url']; //main link
$parse = parse_url($affid);
$host = $parse['host'];
$host = str_ireplace('www.', '', $host);
//flipkart affiliate link generates here
$url_parts = parse_url($affid);
$url_parts['host'] = 'dl.flipkart.com';
$url_parts['path'] .= "/";
if(strpos($url_parts['path'],"/dl/") !== 0) $url_parts['path'] = '/dl'.rtrim($url_parts['path'],"/");
$url = $url_parts['scheme'] . "://" . $url_parts['host'] . $url_parts['path'] . (empty($url_parts['query']) ? '' : '?' . $url_parts['query']); 
$afftag = "harshk&affExtParam1=$subid"; //our affiliate ID
if (strpos($url, '?') !== false) {
if (substr($url, -1) == "&") {
$url = $url.'affid='.$afftag;
} else {
$url = $url.'&affid='.$afftag;
}
} else { // start a new query string
$url = $url.'?affid='.$afftag;
}
$flipkartlink = $url;
//amazon link generates here

$amazon = $affid;
$amzntag = "subhdeals-21"; //our affiliate ID
if (strpos($amazon, '?') !== false) {
if (substr($amazon, -1) == "&") {
$amazon = $amazon.'tag='.$amzntag;
} else {
$amazon = $amazon.'&tag='.$amzntag;
}
} else { // start a new query string
$amazon = $amazon.'?tag='.$amzntag;
}
}
$amazonlink = $amazon;
$cueurl = "https://linksredirect.com/?subid=$subid&source=linkkit&url="; //cuelinks deeplink for redirection
$ulpsub = '&subid=' .$subid; //subid
$encoded = urlencode($affid); //url encode
$home = $cueurl . $encoded; // default link for redirection.
$partner = array( //Insert links here
"amazon.in" => "$amazonlink",
"flipkart.com" => "$flipkartlink",
"shopclues.com" => $cueurl . $encoded,
"aliexpress.com" => $cueurl . $encoded,
"ajio.com" => "https://ad.admitad.com/g/?ulp=$encoded$ulpsub",
"croma.com" => "https://ad.admitad.com/g/?ulp=$encoded$ulpsub",
"myntra.com" => "https://ad.admitad.com/g/?ulp=$encoded$ulpsub",
);
$store = array_key_exists($host, $partner) === false ? $home : $partner[$host]; //Checks if the host exists if not then redirect to your default link
header("Location: $store"); //Do not changing
exit(); //Do not changing
?>

感谢您用您的代码更新您的答案,并解释实际问题是什么。由于您的附属链接引用数组是按基域索引的,我们需要规范主机名以删除任何可能的子域。现在你有:

$host = str_ireplace('www.', '', $host);

显然,只有当子域是www.时,它才能完成任务。现在,人们可能会倾向于将explode简单地替换为.,并取最后两个组件。然而,对于您的.co.id和其他二级域,这将失败。我们最好使用正则表达式。

人们可以创建一个通用正则表达式来处理所有可能的二级域(co.,net.,org.;edu.

$host = preg_replace('#^.*?([^.]+.)(com|id|co.id)$#i',  '12', $host);

为了解释我们使用的正则表达式:

  • ^主题锚点启动
  • .*?任意字符的非自由可选匹配(如果存在子域或子域)
  • .字符后接.(主域名)的([^.]+.)捕获组
  • 域扩展的(com|id|co.id)捕获组(根据需要添加到列表中)
  • $主题锚结束

然后我们将主机名替换为与域匹配的捕获组的内容。及其扩展。这将为www.example.comfoo.bar.example.com-或example.com返回example.com;而example.co.id用于www.example.co.idfoo.bar.example.co.id-或example.co.id。这将有助于您的脚本按预期工作。如果还有其他问题,请更新OP,我们将了解可用的解决方案。

最新更新