Question

I have a website that I will call example.com. This website has several subdomains a.example.com, b.example.com, c.example.com and so on. I want to allow the visitors to the domain to choose a particular subdomain as the preferred page. From this point on when the user visits the page they will be automatically redirected to their preferred page.

I have no clue how to do this or even where to look.

Was it helpful?

Solution

First, are the users registered and logged in or are you identifying them by their IP Address?

Registered and logged in: Log their user ID, email, or whatever you use as the primary key (hopefully ID) in the database in another table with their preferred subdomain when they select their preference.

Not registered / logged in: Log their IP address and their subdomain choice in the table.

Basic table structure could be

id - ip_address(or user_id) - subdomain

Then do an if statement checking the database for their IP or user ID.

<?
    /* database checking code, etc, assuming we're returning $result from the query */
    $subdomain = @mysql_result($result,0,'subdomain');
    if($subdomain != '') header('Location: '.$subdomain.'.yourdomain.com');
?>

That's only one strategy, and probably the most basic. Obviously you'll have to write the code (I'm assuming you know how to do this and want the strategy, otherwise please look at tutorials instead of asking here), and be sure that header is called before absolutely any output is generated (i.e. at the top of your file).

If it fails to find a preference, they just stay on that page.

Half asleep while writing this so I apologize if any information is incomplete or not understandable.

OTHER TIPS

Well, you need

a) a place to store the preference, for example in a cookie (using PHP or Javascript) or in a database (if users are logged in)

b) a page where users can set there preference

c) some script that redirect the user to their prefered site (for example using the header-function in PHP)

Add subdomain url's on a page(settings.php) like below :

<a href="choose.php?sub=abc">Choose ABC Subdomain</a><br/>
<a href="choose.php?sub=bbc">Choose BBC Subdomain</a><br/>
<a href="choose.php?sub=subin">Choose subin Subdomain</a>

choose.php :

<?
if($_GET['sub']!=''){
 $sub=$_GET['sub'];
 setcookie("subwhere",$sub, time()+301014600, "/", "example.com"));
 header("Location:http://$sub.example.com");
}
?>

and on index.php include the checkredirection.php file at the top of the page :

<?
include("checkredirection.php");
?>

checkredirection.php :

<?
$coo=$_COOKIE['subwhere'];
if($coo!="" && $_SERVER['SERVER_NAME']!="$coo.example.com"){
 header("Location:http://$coo.example.com");
}
?>

Include the checkredirection.php on the pages you want the user to redirect.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top