Is there an easy way to swap a file url based on domain name?
I've acme.com & acme.co.uk site domains which require different header graphic but are otherwise identical content. Rather than managing two sets of files and CMS, is there a JS/php script or other method to change the header graphic url dynamically based on which domin name is accessed? Site is basic XHTML & CSS.
With PHP
Use $_SERVER['HTTP_HOST'] to get your domain and then do a switch-statement to change the image-variable.
<?php
switch($_SERVER['HTTP_HOST']) {
case 'acme.com':
case 'www.acme.com':
$image = "acmecom.jpg"
break;
case 'acme.co.uk':
case 'www.acme.co.uk':
$image = "acmecouk.jpg"
break;
default:
$image = "default.jpg"
break;
}
?>
If your using www before acme.com you have to change the address from "acme.com" accordingly to "www.acme.com". Same for .co.uk.
In your header-image you then echo the path to the image like this:
<img src="path/to/folder<?php echo $image;?>" alt="header-image" />
Because I can't stand Tim's coding style...
<?
$host = preg_replace('~^www\.~','',$_SERVER['HTTP_HOST']);
$headerpic = basename($host).".jpg";
if (!is_readable($_SERVER['DOCUMENT_ROOT'])."/images/header/".$filename) {
$headerpic = "default.jpg"
}
?>
<img src="/images/header/<?=$headerpic?>">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With