Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Is there a quicker way to change city names that has the same content with PHP?

Hi Everybody,

I was curious. If I have multiple pages with the same content and each of those pages will have a different city name in the body. How can I get that done?

Do I use a variable? Do I create a page for each city? Or do I use another language?

Thank you

4 Answers

I have done this before using a combination of PHP and HTACCESS rules. I also did the same thing on another server using Classic ASP and HTACCESS rules again.

Do you know if your server supports HTACCESS RewriteRules?

I basically regular expression HTACCESS RewriteRule look for any request for a page with a specific URL structure e.g. http://www.mydomain.com/city/NAMEOFCITY

These all redirected on the server to One Page that took the city name as a variable and using PHP populated the instances on the page where I wanted the data inserted. In my case it also added some customized content from a JSON feed depending on the city name.

Hi Rhian,

Yes it does support HTACCESS Rewrite.

Where would the city name come from? I mean how will it know which city name to use?

In my case, the city name came from the original URL request.

For example, http://www.mydomain.com/local/CT/Greenwich/ or http://www.mydomain.com/CA/Berkeley

I then had a template page http://www.mydomain.com/yourcity/ that expected two parameters (state and city). Based on the structure of the first url requested, I had rewrite rules that would go to http://www.mydomain.com/local/?st=CT&ct=Greenwich . The template page just merged in the parameters. I did do not data manipulation for some locations using php (proper case, spacing for example). You can "hide" the passed through parameters if you want to by either another htaccess rule or an immediate post to another page.

Alternatively, you could try using a IP Geo-localization service (e.g. http://www.ipinfodb.com/) and get the city name from the user's IP address and merge that in using PHP. But the service is not very accurate on a city level. I do use it on a country level though.

Thanks.

Just curious. Does retrieving an IP Adrress by using a IP Geo-localization service like the website you mentioned bad for SEO?

The retrieval from the IP-Geolocalization service happens pre-page load on the server-side (not client side... though you could do it that way too). However, it does result in slower page load speeds. Which could negatively affect SEO.

From a multiple content perspective, it should not as it is the same page. However, you will not be able to control what gets indexed by the search engines / spiders etc.

My city pages actually have very different content (quotes, addresses and other data), so it has not been a problem with regards to duplicate content.

In production, I use the Geo-localization service sparingly. I use it on a product homepage to direct customers to the correct content for their country. Also, I add a cookie to store the user's country so I do not query the service repeatedly during the same user session if they allow cookies; and ideally if they return later and already have a cookie set. This improves load time for our repeat users and limits our requests to the third party service.

travis martin
travis martin
1,804 Points

Hello

I would follow Rhian routing example http://www.mydomain.com/city/cityname you can also just set the $_GET variables by appending this at the end of the URL: ?city=cityname. this is only if you do not want to mess with the HTACCESS file. example below:

file name: example.php

url: http://www.mydomain.com/example.php?city=teamTreeHouse

<?php

//checks if the city variable is set if not it presets it with "not set"
$city = isset($_GET['city']) ? $_GET['city'] : "not set";
echo $city; //teamTreeHouse
?>

That is pretty much exactly the code on my final template page. I just like the "prettiness" of the other url format from a user perspective ;)