dertester2
dertester2

Reputation: 41

Get URL of page without domain using the same site structure

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.

Upvotes: 3

Views: 4395

Answers (1)

IAteYourKitten
IAteYourKitten

Reputation: 1019

<?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;
    }
}
?>
<a href="<?php echo generateLink($url, $uri); ?>">Link</a>

Upvotes: 3

Related Questions