Monday, August 25, 2008

SEO Friendly URLs

Let’s say you have a site which has movie reviews. To display a unique review, you would have a PHP page, for example review.php, which obtains the id of the review and shows the title and description of that review. So, if you want to see the review with id 45, you need to access the following URL:

Do you see a problem with this? No? Are you sure? Ok, then I’ll have to explain it. With that type of URL, you are not able to know anything about the review. It could be a review for “Casablanca” just as easily as it could a review for “Ace Ventura: Pet Detective”. And that’s not the biggest problem; it is also a bad SEO URL!

How can you fix this? Use SEO Friendly URLs.

Requirements: Apache (with mod_rewrite module) installed on your server (most hosting services already have this).

Step 1: Insert the following code on you PHP script

function StrToSearchFriendlyURL($s) {
if(!$s) return 'page';

$s = strtolower(htmlentities($s, ENT_QUOTES, $GLOBALS['CHARSET']));
$s = preg_replace('/&(.)(?:acute|cedil|circ|ring|tilde|uml|grave|elig|slash);/', '\\1', $s);
$s = preg_replace('/\W+/', '_', html_entity_decode($s));
$s = preg_replace('/_{2,}/','_',$s);
$s = trim($s,'_');

if(!$s) $s = 'page';

return $s;
Step 2: Apply the above function to your regular URLs to make them SEO Friendy.

Step 3: When inserting the new URLs on your page, add a dash (-), the review id and html extension at the end.


<a href="{SEO_friendly_URL}-{id}.html">{$review_title}</a>

Step 4: Create a .htaccess file with the following content.

Options +FollowSymLinks

RewriteEngine On
RewriteRule ^[^-]+-([0-9]+)\.html$ review.php?id=$1 [L]

Step 5: Place the .htaccess file on your site’s document root.

That’s it! The URLs on your site will now look something like this: which will be interpreted by Apache like

More about mod_rewrite:
More about .htaccess files:

Friday, August 22, 2008

Obtaining a visitor’s country from its IP address

Sometimes, when you are building a site, you may want the content of the homepage or even the language to be used, to change depending on the country from which the visitor is accessing.

How can you do this? It’s actually pretty simple, just follow these steps:

1. Get an IP-Country database. Each row of these databases often has three columns: start and end range for the IP address and the country to which that range is associated (e.g. Most of these databases are not free, and the ones that are, are generally a little outdated. I recommend purchasing a database, mainly because you can download a new updated version every month.

2. Import the IP-Country database into the database on your site. The IP-Country databases are generally in the .csv format (comma separted values), so you have to execute a query to import all the values. That query should look something like this:

LOAD DATA LOCAL INFILE '/ip-country.csv'
INTO TABLE ip_country
(start_range, end_range, country);

3. Include the following PHP code on the page that you want to use to obtain the Country:

$IPaddr = "";
if (getenv("HTTP_X_FORWARDED_FOR")) {
$IPaddr = getenv("HTTP_X_FORWARDED_FOR");
} else {
$IPaddr = getenv("REMOTE_ADDR");
$ips = split ("\.", "$IPaddr");
$ip_address = ($ips[3] + $ips[2] * 256 + $ips[1] * 256 * 256 + $ips[0] * 256 * 256 * 256);
$res = $db->query($sql);
$row = mysql_fetch_row($res)
$country = $row[‘country’];

That’s it! You can now obtain the name of the country from which you visitor is accessing from and display the appropriate content.

P.S: If you think is paying me for each database bought from this Blog, you are wrong!

P.S2: If you are working at, please consider paying me for all the referrals!

Tuesday, August 19, 2008

About this Blog

Welcome to the Struggling Developers Blog!

My name is Pablo, I have a small software development company together with two partners, and we have been struggling for about a year to keep our company alive. The three of us used to work as employees on different software companies until we decided to open our own. Since then, we had to deal with different types of projects, technologies and clients.

In this Blog I will tell you about the obstacles that we had to face as a company, with both programming and clients, so that maybe you can learn from our experiences (and the lessons we got from them) and use them on your professional life.

I know I might not have the best solution for every problem, so if you have something to add or a different approach for any of my posts, please leave a comment. I promise I’ll read all of them and (if needed) update my posts with your feedback.