URL customisation rewrite? www.url.co.uk/?brand=branda

It's called URL rewriting.

Are you using PHP/ASP.Net Webforms or MVC?

*edit*

Actually, I lied, because you have the ? in the URL, you're just passing the brand in the querystring. It's just your common or garden GET request.

To get the variable in PHP it's:
Code:
$brand = $_GET['brand'];

In ASP.NET Webforms it's
Code:
string brand = Request.Querystring["brand"];

And in MVC you'd set up your routes to automatically pass the value to your actions.
 
Last edited:
Thanks its using PHP

(You'd probably gathered I'm not a coder)

So how does the URL command then talk to the stylesheet to tell it that it wants a different brand logo? does it replace a div name on the page to do this?

Thanks
 
Well, you want something like this:


Code:
<?php if($_GET['brand'] == 'A') : ?>

     <img src="http://link-to-my-site.com/image1.jpg" alt="My Image  Description" />

<?php elseif($_GET['brand'] == 'B') : ?>

     <img src="http://link-to-my-site.com/image2.jpg" alt="My Image  Description" />

<?php else : ?>

          <img src="http://link-to-my-site.com/default-image.jpg" alt="My Image  Description" />

<?php endif; ?>
 
You can do it a few ways.

You can either do it with CSS, or if it's purely the logo you want to change, do something like this:

This goes where the Logo HTML currently is:
Code:
<?php

$brand = $_GET['brand'];

echo "<img src=\"images/". $brand .".jpg\" alt=\"Website Logo\" />";

?>
and name the logos something like "a.jpg", "b.jpg" in the folder named "images" (change to suit your needs) ... Of course this isn't the most secure way (taking variables from URL's unvalidated)

In the case of there being no variable set, you wont get an image at all - a way around this would be:

Code:
<?php

$brand = $_GET['brand'];

if($brand) {
echo "<img src=\"images/". $brand .".jpg\" alt=\"Website Logo\" />";
} else {
echo "<img src=\"images/default_logo.jpg\" alt=\"Website Logo\" />";
}

?>

This will check to see if a variable has been set. If it has, it shows the appropriate logo, if not, it shows an image named "default_logo.jpg"


:edit:

Or the way Edward mentioned :D
 
I wouldn't ever put something straight from the querystring into the page. You're opening up a massive security hole.

Instead use it as a flag, something like this:

Code:
$brand = $_GET['brand'];
$logo = "default.jpg";
if ($brand == "A") {
    $logo = "logoA.jpg";
}
else if ($brand == "B") {
    $logo = "logoB.jpg";
}

<img src="<?= $logo ?>" alt="Logo" />
 
Hi thanks for your help guys sorry for the late reply - on the last reply from Spunkey - where would I reference the location of the logos storred? would I put the full url or dir in the areas you currently have logoa.jpg?
 
You could do it like that

Code:
<?php
$brand = $_GET['brand'];
$logo = "default.jpg";
if ($brand == "A") {
    $logo = "http://www.mysite.com/images/logoA.jpg";
}
else if ($brand == "B") {
    $logo = "http://www.mysite.com/images/logoB.jpg";
}
?>

<img src="<?php echo $logo; ?>" alt="Logo" />


This would be useful if the images were in different locations. If they are in the same location (e.g, images/) you could do this (path after "src"):

Code:
<?php
$brand = $_GET['brand'];
$logo = "default.jpg";
if ($brand == "A") {
    $logo = "logoA.jpg";
}
else if ($brand == "B") {
    $logo = "logoB.jpg";
}

?>

<img src="images/<?php echo $logo; ?>" alt="Logo" />


I changed the php tags, just as habit as this is how i've always done it, and some hosts don't support "short tags" (<? instead of <?php)
 
Last edited:
Can this method also work to swap a DIV out instead? IE if I have a div that specifies the correct formatting and logo within it - instead of just swapping out the .jpg or .png file can it just replace the div id like

<div id="logo1">
<div id="logo2>

ect?
 
Surely you'd be better off url rewriting it though?

I'm struggling to think of a situation where branding depending on querystrings is the best or even a good solution.
 
Works a charm thanks Spunkey - whats the cleanest way to put in a default div like the example above with the default $logo
 
Surely you'd be better off url rewriting it though?

I'm struggling to think of a situation where branding depending on querystrings is the best or even a good solution.

Rewriting is pretty much irrelevant.. it'll still be a querystring or at best a (v)host name check.

Cleanest way to do this is to use an array/dictionary of known items, and if the supplied item isn't in there, then use a default.

Has been a LONG time since I have typed any PHP, so forgive any syntactical querks:
PHP:
<?php

$known_logos = array (
  'a' => 'logo_a.jpg',
  'b' => 'logo_b.jpg',
  '..' => 'some_other_file.ext',
  'z' => 'lalalalalal.gif');

$default_logo = 'foo.bar';

$logo_to_use = isset($_GET['brand']) && isset($known_logos[$_GET['brand']]) 
  ? $known_logos[$_GET['brand']] 
  : $default_logo;

echo '<img src="/arbitrary/path/to/images/' + $logo_to_use + '" alt="logo"/>';

?>
 
How would I do what Jestar suggested but with Div's not direct image links?

I've tried:

<?php

$known_logos = array (
'a' => '<div id="a"></div>',
'b' => '<div id="b"></div>');

$default_logo = 'a';

$logo_to_use = isset($_GET['brand']) && isset($known_logos[$_GET['brand']])
? $known_logos[$_GET['brand']]
: $default_logo;

?>
 
I'm trying to rewrite with the htaccess but not having much luck - not sure if I'm setting my rule backwards or not but either don't seem to work:

I want:

www.example.co.uk/first/second/index.php?brand=a

to rewrite to

www.example.co.uk/first/a/second

so that I can use the neater url to show people the correct page

This is the rule I thought would work:

RewriteEngine on

RewriteRule ^first/a/second$ first/second/index.php?brand=a
 
Back
Top Bottom