Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Script to change webpage header based on domain name

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.

like image 670
Wes79 Avatar asked Jan 22 '23 09:01

Wes79


2 Answers

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" />

like image 193
Tim Avatar answered Feb 08 '23 15:02

Tim


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?>">
like image 32
Your Common Sense Avatar answered Feb 08 '23 16:02

Your Common Sense