Click to See Complete Forum and Search --> : virtual subdomains
frank1002
03-19-2006, 09:31 PM
i really do not know if this is a good place to post this question or not but here i go
i have a mini mall that its url is something like
for first_store mysite.com?store=1
for second store mysite.com?store=2
and so on
i want to have this changed to
first_store.mysite.com which will redirect to mysite.com?store=1
i can do this with creating subdomains in my cpanel and having each one redirect to the correct store
but i was looking to do this without actually creating the subdomain and folders
and hopefully dynamic
i mean if customer comes to first_store.mysite.com
a search for the correct store_id in the mysql and redirecting to the appropiate store
if someone can give me some advice and point me toward some reading material it would be great
SpectreReturns
03-19-2006, 09:43 PM
The apache functions allow you to work with apache dynamically (maybe let you set up subdomans?). The other possibility is hooking up this functionality to a 404 page, so you hit a non-existant subdomain, it checks where you want to go, and redirects you there.
bokeh
03-20-2006, 04:45 AM
Pretty simple really. Set up a wildcard sub domain (*.domain.com) and have it point into your domain root directory. This means all requests for subdomain.domain.com go straight to the file to domain.com/index.php without any redirect or rewriting. You can do this easily with cpanel. Now all you need to do is have index php filter the incoming requests. Something like the following should work fine for that:<?php
# database connect stuff here
#test for a sub domain (this code works for a TLD i.e. "domain.com")
$host_exploded = explode('.', $_SERVER['HTTP_HOST']);
if((count($host_exploded) > 2) and (strtolower($host_exploded[0]) != 'www'))
{
# we have a sub domain
$query = sprintf("SELECT * FROM `stores` WHERE `store_name` = '%s'", mysql_real_escape_string(strtolower($host_exploded[0])));
$query_result = mysql_query($query) or die(mysql_error());
if(mysql_num_rows($query_result) > 0)
{
# the store exists (sub domain good)
while($row = mysql_fetch_assoc($query_result))
{
# do something usuful with database result
}
}
else
{
# the store does not exist (sub domain bad)
# take relevant action
}
}
else
{
# this is a standard request (no sub domain)
# take relevant action
}
?>
frank1002
03-22-2006, 07:22 PM
great thanks bokeh everthing works except one thing
problem i am having is when there is no subdomains
i want to do diffrent things if the user click on a link or if he just enters for the first time
in another word i need to diffrentiate if anything is after .com
mysite.com be in store1 and
mysite,com/index.php?product_id=1 be in whatever store he was in
i would appreciate if you can help me out
bokeh
03-22-2006, 08:31 PM
Can you expand on that. I'm having trouble grasping what you are asking.
frank1002
03-22-2006, 10:11 PM
i have changed this part of your code
# this is a standard request (no sub domain)
# take relevant action
$mystore_id=0;
#end of action if no subdomain
but i want it to set $mystore_id=0; only if there is nothing after mydomain.com
now when user clicks a link in a subdomain he is redirected to the mysite.com/store_id.php and then it resets $mystore_id=0;since it does not find a subdomain