How do I make a website with subdomains catering to different cities?
Like http://philly.example.com
, http://maine.example.com
, http://sandiego.example.com
? Most of the site will be the same, like the layout, the wordings, the database, the interface. What will be different will be some city-specific graphics like adding a city name to the logo (eg. Groupon Nyc, Groupon Chicago), changing a hidden variable (city) in the form that searches the database so results will be biased towards the city.
Question 1. User visits site. Script determines city from user's IP address then redirect to the correct subdomain.
I can do the detection script, but how do you display almost the same site to each of the different city? I guess you do not duplicate the site 50 times for 50 different cities, handling the changes to every site will be insane!
Question 2: Assuming you do not duplicate the site each time for a new city and change the images and text slightly, how do you do the city-specific changes then?
Do you have a list of 50 case-ifs
or if-elseif-else
for every city-specific item on the page? I foresee doing this will mess up the code for the entire page!
Q1) You have the cities in a database and then whatever you want to show, you do by cityid.
Q2) Again, show data by cityid.
It's fairly simple with a wild-card DNS/web setup.
*.example.com
to your server's IP address<VirtualHost IP:80>ServerAlias *.example.com example.com
$_SERVER['HTTP_HOST']
to figure out which sub-site the user is accessing, and tailor your content from there.It really depends on how much of your information is different per city. It seems like you have really three choices.
SANDIEGO -> logo -> "img/sandiego.gif"
)Pretty much when person loads the page you check where they are - you might want to do "Are you sure" sort of dialog where it asks that they are in fact in the city you have detected.
If you go with option 1, all you do is you change currently used db to city specific db. For option 2 you just setup global cityId, so all queries are made to that specific city.
To do this you could create two tables.
Table 1:
id INT Primary key
city_name VARCHAR 255
Table 2:
id INT Primary key
city_id INT Foreign key
page_id INT
page_content TEXT
There are a bunch of different ways of doing it. It depends whether EACH city is going to have different content, or just a select few. Otherwise it will be more technical as you need to determine which content will apply for which city. This would require tweaks to your sql tables.