Get URL of page without domain using the same site structure - php

I have the following problem: I have a set of domains with the same url structure:
domain-a.com/london/
domain-b/london/
domain-c/london/
I want to do the following thing:
If you are on domain-a.com/london/, I want "related" links underneath pointing to domain-b.com/london/ and domain-c.com/london/
I want these links to appear automatically using the URL of the current page, remove the domain so that only the rest is left - in my example: /london/ and add the other domains in front of this.
I know I have to use echo $_SERVER['REQUEST_URI']; to get the rest of the URL but I don't know how to create a link using this function.

<?php
$url = $_SERVER['HTTP_HOST'];
$uri = $_SERVER['REQUEST_URI'];
function generateLink($url, $uri){
if(strpos($url,'domain-a.com') !== false){
$link = 'http://domain-b.com' . $uri;
return $link;
}else if(strpos($url,'domain-b.com') !== false){
$link = 'http://domain-c.com' . $uri;
return $link;
}else if(strpos($url,'domain-c.com') !== false){
$link = 'http://domain-a.com' . $uri;
return $link;
}
}
?>
Link

Related

Remove subdomain from URL/host to match domains in affiliate link array

I want to make a redirect file using php which can add Affiliates tag automatically to all links. Like how it works https://freekaamaal.com/links?url=https://www.amazon.in/ .
If I open the above link it automatically add affiliate tag to the link and the final link which is open is this ‘https://www.amazon.in/?tag=freekaamaal-21‘ And same for Flipkart and many other sites also.
It automatically add affiliate tags to various links. For example amazon, Flipkart, ajio,etc.
I’ll be very thankful if anyone can help me regarding this.
Thanks in advance 🙏
Right now i made this below code but problem is that sometimes link have extra subdomain for example https://dl.flipkart.com/ or https://m.shopclues.com/ , etc for these type links it does not redirect from the array instead of this it redirect to default link.
<?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
?>
Thank you for updating your answer with the code you have and explaining what the actual problem is. Since your reference array for the affiliate links is indexed by base domain, we will need to normalize the hostname to remove any possible subdomains. Right now you have:
$host = str_ireplace('www.', '', $host);
Which will do the job only if the subdomain is www., obviously. Now, one might be tempted to simply explode by . and take the last two components. However that'd fail with your .co.id and other second-level domains. We're better off using a regular expression.
One could craft a universal regular expression that handles all possible second-level domains (co., net., org.; edu.,...) but that'd become a long list. For your use case, since your list currently only has the .com, .in and .co.in domain extensions, and is unlikely to have many more, we'll just hard-code these into the regex to keep things fast and simple:
$host = preg_replace('#^.*?([^.]+\.)(com|id|co\.id)$#i', '\1\2', $host);
To explain the regex we're using:
^ start-of-subject anchor;
.*? ungreedy optional match for any characters (if a subdomain -- or a sub-sub-domain exists);
([^.]+\.) capturing group for non-. characters followed by . (main domain name)
(com|id|co\.id) capturing group for domain extension (add to list as necessary)
$ end-of-subject anchor
Then we replace the hostname with the contents of the capture groups that matched domain. and its extension. This will return example.com for www.example.com, foo.bar.example.com -- or example.com; and example.co.id for www.example.co.id, foo.bar.example.co.id -- or example.co.id. This should help your script work as intended. If there are further problems, please update the OP and we'll see what solutions are available.

I want to redirect website visitor from "expl.com/... "to "expl.com/index.php/..."

This is my domain http://cdtr.cf it lands on index.php
Where I wrote some code which pulls the URL from URL box and trim its suffix to identify visitor's identity.
The Problem is when someone opening http://cdtr.cf/.... it shows error 404 page not found
while just http://cdtr.cd/ is working fine. All I need is When someone visit with http://cdtr.cf/.... they must be redirected to index.php and their request should be processed like http://cdtr.cf/index.php/.....
I want to keep the suffix anyhow for some purpose :-http://cdtr.cf/suffix.
It will be great if all this happen without any visible change in URL box.
Thanks in advance.
$link= (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']=== 'on'? "https" : "http") . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
echo $link; //It is working fine on localhost but not when i put it live
This code will give you the rest of the url as per your requirement
<?php
$link = $_SERVER['REQUEST_URI'];
echo $link = ltrim($link, '/');
//if you want you can trim last / too using $link = rtrim($link, '/');
?>
If you want to get middle stuff like this yoursite.com/stuffs/removedthis
then you have to use below code
<?php
$link = $_SERVER['HTTP_HOST'];
$link .= $_SERVER['REQUEST_URI'];
$link = explode('/', $link);
echo $a = $link[1];
?>
Second example:
$link = "example.com/stuff/removethis/....blah";
$link = explode('/', $link);
echo $a = $link[1];

Creating an if statement for an included menu

I have a menu that is consistent throughout one of my directories. For example,
Main Page
Documents Page
Photos Page
I would expect to be able to include a menu like this into each page, and I'd be done. However, this time it's a bit different, because the links generated by individual pages have to be different even though the destination is the same. Take the link to the galleries.php page as an example:
From the Home page: <a href='galleries.php?id=<?=$id?>'>
From the Documents page: <a href='galleries.php?id=<?=$doc[lid]?>'>
From the Photos page: <a href='galleries.php?id=<?=$photo[lid]?>'>
From the Productss page: <a href='galleries.php??id=<?=$product[lid]?>'>
>
What I'm doing for now is to copy and paste the menu into each file, and changing the URL as needed, but this isn't a very satisfactory solution. How can I build some sort of if statement in the menu itself so the correct link is generated by the page that is including the menu?
$_SERVER['REQUEST_URI'] can give you the page you're currently on, and you can use this to determine where you should link to.
Example:
function displayGalleryId($lid) {
$uri = $_SERVER['REQUEST_URI'];
switch ($uri) {
case '/home.php':
$link = $id;
break;
case '/documents.php':
$link = $doc[$lid];
break;
// Others here...
default:
$link = 'gallery.php';
}
return $link;
}
Example Usage:
<a href='galleries.php?id=<? displayGalleryId($lid); ?> '>
You need to use $_SERVER global array..
you can use $_SERVER['SCRIPT_FILENAME'] or $_SERVER['REQUEST_URI']
it can be like
function getLink($id)
{
$uri = $_SERVER['REQUEST_URI'];
$is_page_home = (strstr($uri, 'home') === true)?true:false;
$is_page_photos= (strstr($uri, 'photos') === true)?true:false;
$is_page_document = (strstr($uri, 'document') === true)?true:false;
if( $is_page_home )
{
$urlid = $id
}
if( $is_page_photos)
{
$urlid = $doc[$id]
}
if( $is_page_document )
{
$urlid = $photo[$id]
}
return $urlId
}
$urlId = getLink($id)
<a href='galleries.php??id=<?=$urlId?>'>
to learn more about server variables http://php.net/manual/en/reserved.variables.server.php

Get URL with an if statement PHP

I am trying to get URL path and to save it as variable...
$setURL = true;
$getDomain = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$getSubdomain = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
if ($setURL === true) {
$result = 'http://'.parse_url($getDomain, PHP_URL_HOST) . '/';
echo 'get domain';
} else {
$result = 'http://'.parse_url($getSubdomain, PHP_URL_HOST).parse_url($getSubdomain, PHP_URL_PATH);
echo 'get subdomain';
}
$siteURL = $result;
So basically if I defined variable $setURL = true; it will return correct URL for simple domain ... http://domain-name.com
However else does not work as I want to... else is there for subdomains. So if I set $setURL = false; it should return following... http://domain-name.com/path/
But unfortunately it return more then that... It returns anything I type as URL...
http://domain-name.com/path/something/index.php it will return all of that as URL!
Please help me to fix this as I don't have any ideas how I could manage to make it.
Formally, a subdomain precedes a domain name. For example, in ftp.debian.us, ftp is the subdomain.
It sounds like what you want is the first path in the URI. You can use PHP's explode() method to grab the first segment in the path.
$uriparts = explode($_SERVER['REQUEST_URI']) // = '/path/to/somewhere/index.html'
$path = $uriparts[1] // = 'path'

Grabbing Refferer's URL - PHP

I am trying to grab statistics on my site and I need a way in PHP to assign just the referrer URL to a variable. All I want is just the "www" or subdomain, the domain, and the tld... I.E. www.facebook.com instead of http://www.facebook.com/BLAH-BLAH/?blah=blah
Try something like the following:
$referer = $_SERVER['HTTP_REFERER'];
$parts = parse_url($referer);
echo $parts['host']; //should echo example.com, or whatever
<?php
$ref = $_SERVER["HTTP_REFERER"];
$host = null;
if ($ref != null) {
$parse = parse_url($ref);
$host = $parse["host"];
}
?>
and then you have $host to work with

Categories