Jump to content

Change logo based on incoming domain


EFitzpatr7

Recommended Posts

I am posting here for generic coding support rather than WordPress. I originally posted this on the WordPress Development Stack Exchange and was supported until it was no longer a WordPress related matter.


 


The agency I work for are trying to get a client's logo to change based upon the incoming URL.


 


Now, we have 4 different URLs, therefore 4 different logos.


 


Currently, we are using an if/else statement, and the default logo is being bought through to the header.


 


The if/else statement we are using is as follows:



<?php
if(!empty($image)) {
echo '<h1>WEBSITE_NAME_HERE</h1>';
}
else {
echo '<img src="'.logoSwap().'" alt="Localised Logo">';
}
?>

The problem now lies with the logo not actually changing.


 


We are using a switch/case statement to actually specify the image paths and the incoming domains, and in order for the logoSwap() function to actually be declared:



<?php
function logoSwap(){
switch($_SERVER['HTTP_HOST']) {
case 'WEBSITE_URL_HERE.com':
$logo = "WEBSITE_URL_HERE/wp-content/uploads/2016/01/logo-red.png";
break;
case 'www.WEBSITE_URL_HERE.com':
$logo = "WEBSITE_URL_HERE/wp-content/uploads/2016/01/logo-red.png";
break;
case 'WEBSITE_URL_HERE.co.uk':
$logo = "WEBSITE_URL_HERE/wp-content/uploads/2015/09/logo.png";
break;
case 'www.WEBSITE_URL_HERE.co.uk':
$logo = "WEBSITE_URL_HERE/wp-content/uploads/2015/09/logo.png";
break;
default:
$logo = "WEBSITE_URL_HERE/wp-content/uploads/2015/09/logo.png";
break;
}
return $logo;
}
?>

The .com domain is using a 301 redirect which is pulling it directly to the .co.uk domain, however the logo isn't changing when accessed from the .com domain. Could it simply be the fact that it is coming in via a 301 redirect?


 


For example, I will type in mywebsite.com and I will get redirected to mywebsite.co.uk, however it displays the same logo on both.


 


It has been suggested to me to set a cookie (i.e. com=1) on the redirect, and then simply do the logoSwap() function as a simple "if cookie set, show red, else show normal" code.


 

Unfortunately, I don't quite know how I'm going to do this, so this is where I need your help.

 

Any help would be much appreciated.

 

Link to comment
Share on other sites

If there are any redirections then it should be easy to spot: go to one domain, see if you're redirected to another domain.

 

But if you don't have those redirections then you'll suffer with SEO: having four domains with the same content is not good. You should pick one and go with it, either by redirecting everyone to it or using a rel=canonical to point to the "main" domain's version of the page. Either way, though, search engines will only show one of the sites in their search results.

 

Varying the image on www or non-www domains is... silly. I have no idea why you would want to do that. Varying by .co.uk or .com? Sure. But that sounds like an internationalization thing, and those kinds of issues are handled differently than showing the same site on multiple domains.

Link to comment
Share on other sites

If there are any redirections then it should be easy to spot: go to one domain, see if you're redirected to another domain.

 

But if you don't have those redirections then you'll suffer with SEO: having four domains with the same content is not good. You should pick one and go with it, either by redirecting everyone to it or using a rel=canonical to point to the "main" domain's version of the page. Either way, though, search engines will only show one of the sites in their search results.

 

Varying the image on www or non-www domains is... silly. I have no idea why you would want to do that. Varying by .co.uk or .com? Sure. But that sounds like an internationalization thing, and those kinds of issues are handled differently than showing the same site on multiple domains.

 

What I meant was I wanted to vary the logo based upon the .co.uk or the .com domain.

 

For example, we have:

 

  • domain1.co.uk
  • domain1.com
  • domain2.co.uk
  • domain2.com

 

We want to have a different logo for each of those 4 domains without duplicating the website. Is this possible?

Link to comment
Share on other sites

Without duplicating the website? I don't know any way for you to keep four distinct sites, with support from search engines, without being penalized for duplication, in a way that you could feasibly have four logos. Duplicating the website is the only way you could have the four sites so each can have their own logo.

 

Internationalization is probably the answer here. Why do you need four domains? What's the difference between them? You know that domain1 vs domain2 and .com vs .co.uk doesn't matter in any technical ways, right? One of those four combinations will be the most appropriate domain name for you to use.

 

If you want to vary the logo but are willing to give up the association with the domain name, you could vary it by the user's IP address: domain1/2.com for US visitors, domain1/2.co.uk for non-US visitors. Or something like that.

Link to comment
Share on other sites

Without duplicating the website? I don't know any way for you to keep four distinct sites, with support from search engines, without being penalized for duplication, in a way that you could feasibly have four logos. Duplicating the website is the only way you could have the four sites so each can have their own logo.

 

Internationalization is probably the answer here. Why do you need four domains? What's the difference between them? You know that domain1 vs domain2 and .com vs .co.uk doesn't matter in any technical ways, right? One of those four combinations will be the most appropriate domain name for you to use.

 

If you want to vary the logo but are willing to give up the association with the domain name, you could vary it by the user's IP address: domain1/2.com for US visitors, domain1/2.co.uk for non-US visitors. Or something like that.

The reason why we wanted to do it on a per-domain basis is for the sheer fact that the logos have the TLD within them. For example, the logo shows WEBSITE_NAME_HERE.co.uk / WEBSITE_NAME_HERE.com

 

However, the company in question have 2 different company names, each with 2 logos.

 

The business itself is based in the UK, so domain1.co.uk would be the primary domain, but the client has asked for each of the logos to be used when the user enters a specific domain, which is where the 3 extra domains come in.

 

I have been provided with a code to redirect the URL within the PHP rather than creating a 301 with the domain registrar. Would this complete the desired task?

<?php
session_start();
$_SESSION['url'] = $_SERVER['HTTP_HOST'];

// redirect done via PHP
if ($_SERVER['HTTP_HOST'] == 'www.WEBSITE_URL_HERE.com') {
  header("HTTP/1.1 301  Moved Permanently');  
  header("location: http://www.WEBSITE_URL_HERE.co.uk');
  exit;
}

function logoSwap(){ 
switch($_SESSION['url']) { 
case 'WEBSITE_URL_HERE.com': 
$logo = "WEBSITE_URL_HERE/wp-content/uploads/2016/01/logo-red.png"; 
break; 
case 'www.WEBSITE_URL_HERE.com': 
$logo = "WEBSITE_URL_HERE/wp-content/uploads/2016/01/logo-red.png"; 
break; 
case 'WEBSITE_URL_HERE.co.uk': 
$logo = "WEBSITE_URL_HERE/wp-content/uploads/2015/09/logo.png"; 
break; 
case 'www.WEBSITE_URL_HERE.co.uk': 
$logo = "WEBSITE_URL_HERE/wp-content/uploads/2015/09/logo.png"; 
break; 
default: 
$logo = "WEBSITE_URL_HERE/wp-content/uploads/2015/09/logo.png"; 
break; 
} 
return $logo; 
} 
?>
Edited by EFitzpatr7
Link to comment
Share on other sites

It's not that simple. Sessions are driven by cookies, and cookies are tied to domains. Each of the four domains will have its own session* because they each have their own cookies. And you cannot set a cookie for a different domain. There is a way to do it with sessions, but it sucks.

 

Easier would be to reuse the redirection.

 

Um... Take this code with a grain of salt: I feel like I'm making a mistake or two somewhere.

 

First, move the redirection stuff out of PHP and into the web server configuration. It will be faster and more efficient if PHP doesn't get involved in the process.

// vhost config (best) or .htaccess (okay)
RewriteEngine on
RewriteCond %{HTTP_HOST} !=www.WEBSITE_URL_HERE.co.uk
RewriteRule ^ http://www.WEBSITE_URL_HERE.co.uk/redirect?site=%{HTTP_HOST}&to=%{REQUEST_URI} [L,R=301]
If the host name doesn't match, redirect to the right hostname and pass along the original hostname and the original URL.

This introduces a new page dedicated just to supporting this stuff. Don't need to bog down the rest of the site with checks if you don't need to.

 

Then write that new page. It doesn't have to output anything because it will always redirect the user somewhere.

$sites = array(
	'WEBSITE_URL_HERE.com',
	'www.WEBSITE_URL_HERE.com',
	'WEBSITE_URL_HERE.co.uk',
	'www.WEBSITE_URL_HERE.co.uk'
);
if (isset($_GET['site']) && in_array($_GET['site'], $sites)) {
	// session_start(); if it hasn't happened yet
	$_SESSION['site'] = $_GET['site'];
}

if (isset($_GET['to'])) {
	// need to validate it so people can only be redirected within this website
	$to = $_GET['to'];
	$todata = parse_url($to);
	// don't allow something with a hostname. both "http://example.com" and "//example.com" will do that
	if ($todata && empty($todata['host'])) {
		header("Location: {$to}", true, 307);
		exit;
	}
}

// if we're here then this page isn't being used correctly
header("Location: /", true, 307);
exit;
Finally there's the logo function. Basically the same as before.

function logoSwap(){
	switch(isset($_SESSION['site']) ? $_SESSION['site'] : $_SERVER['HTTP_HOST']) {
		case 'WEBSITE_URL_HERE.com':
			return "WEBSITE_URL_HERE/wp-content/uploads/2016/01/logo-red.png";
		case 'www.WEBSITE_URL_HERE.com':
			return "WEBSITE_URL_HERE/wp-content/uploads/2016/01/logo-red.png";
		case 'WEBSITE_URL_HERE.co.uk':
			return "WEBSITE_URL_HERE/wp-content/uploads/2015/09/logo.png";
		case 'www.WEBSITE_URL_HERE.co.uk':
		default:
			return "WEBSITE_URL_HERE/wp-content/uploads/2015/09/logo.png";
	}
}
Oh. I renamed the thing from "url" to "site" because it's a bit less ambiguous that way. Probably should go with "original site" or something even clearer.

 

PS: The logo with the different domain names doesn't make sense anymore. If someone goes to domain1.com then they'll be redirected to domain1.co.uk but see the .com logo. That's confusing.

 

* Or two. The www and non-www can share cookies if the session cookie parameters are configured properly, but the .com and .co.uk definitely cannot.

Link to comment
Share on other sites

Thank you for the code - I will look into implementing it later on once I get a spare moment.

 

PS: The logo with the different domain names doesn't make sense anymore. If someone goes to domain1.com then they'll be redirected to domain1.co.uk but see the .com logo. That's confusing.


* Or two. The www and non-www can share cookies if the session cookie parameters are configured properly, but the .com and .co.uk definitely cannot.

 

Unfortunately this was the client's idea, not ours. I understand the confusion that it will cause.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.