Creating a city-specific website (city1.example.com, city2.example.com) [closed]
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 bi开发者_JS百科ased 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!
It's fairly simple with a wild-card DNS/web setup.
- Set your domain's DNS to map
*.example.com
to your server's IP address - Set Apache to do wildcard vhost:
<VirtualHost IP:80>ServerAlias *.example.com example.com
- In your PHP script, you refer to
$_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.
- Separate databases (exactly the same structure but different data)
- One database with table called CityBasedContent (with CityId and content and contentkey eg.
SANDIEGO -> logo -> "img/sandiego.gif"
) - You only change few images maybe some css for colours - ie. not many changes between them. I would just do what @Marc B suggested and/or put css/img in folders based on their cityId. As it would make switching easier.
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.
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.
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
- You determine what city the user is from and grab the city ID from table1
- Select the content related to that city in table 2 using the foreign key from table 1
- To handle images you could fetch your images based on a strict naming convention (eg. img-brisbane-header.jpg, img-cairns-header.jpg) making a different image for each city.
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.
精彩评论