Creating Search Engine Friendly URLs with PHP

In this article we will help you understand the difference between static URLs and dynamic URLs and the benefits of coding search-friendly URLs. We’ll also explain why it is important to optimize URLs and how they impact search engine click through rates. We will introduce you to the basics of PHP URL structure, the mod_rewrite and how to use it to produce static-looking search engine friendly links.

The article only touches on the basics and assumes you have a general knowledge of PHP, as well as some knowledge of SEO.

Why is it Important to Optimize URLs?

In the earlier days of search engine optimization, keywords in your URL helped pages rank higher in search results. Today URLs have little or no impact on search engine rankings; however, URL optimization is still crucial for several reasons.

  • Keywords in URL increase your click through rate and the perceived relevancy of the result.

  • URLs are often cited as the anchor text, so having keywords will improve the value of inbound links.

  • Short, keyword-rich URLs are easier to remember.

  • Most importantly, optimized PHP URLs prevent duplicate content and help search engine spiders crawl better.

URLs contribute to search engine click through rates more than you think. Users are more likely to click on a search result that is more relevant, and the URL string takes up to 20% of your search listing real estate.

Here is an example of a search result:

Title – We Sell Cats

Meta description or snippet from the page – we sell a lot of cats

url string – www.wesellcats.com/cats_for_sale.html

To increase the relevancy of the result you can include keywords in the URL string. Search engines will also highlight keywords in the URL.

Take the following examples. Which one looks more relevant if you searched with “buy Acura TL”?

  • http://example.com/cars/acura_tl.html

  • http://example.com/cars/model.php?id=3&product_id=4

Test performed by search engine marketers found that bolded keywords in the URL increase CTR, so URL optimization can add traffic to the bottom line.

URLs

URLs can be either static or dynamic. Static URLs lead to the actual file on the server, without a query string. PHP URLs can be static, just like HTML URLs. Dynamic URLs, on the other hand, include a query string which specifies parameters for the PHP script. Signs of a dynamic URL are characters in the URL such as ? and &.

http://example.com/cars.php?category_id=3&product_id=4&class_id=4

The “cars.php” is the actual file on the server which contains content. Depending on the parameters following after the “?” and specified with “&”, PHP script is instructed to show only a specific part of the content. Depending on the string parameters, PHP script will show different parts of the content. Even though “cars.php” is the main file that contains all of the content for all URLs, search engines treat strings with different parameters as separate URLs.

Sometimes string instructions do not specify changes in content, but only add parameters (such as session IDs and tracking).

http://example.com/cars.php?category_id=3&product_id=4&class_id=4&parameter=34

http://example.com/cars.php?category_id=3&product_id=4&class_id=4&another_one=56

Though both URLs show exactly the same content, search engines treat those URLs as completely different pages. This is the major cause of duplicate content on the web, and it can hurt overall site rankings.

Stay away from session IDs at all costs. Though a perfect marketing measurement, session IDs create an infinite amount of duplicate pages, causing search engine bots to drop the crawl process altogether or mark the site as a spam factory. If tracking is absolutely necessary, use Omniture Analytics. They have neat feature sets, with the ability to follow users around.

If your URLs are spidered and indexed properly, there is no need to restructure them. Keywords in the URL do not add much to rankings. Restructuring is only necessary if search bots have trouble spidering your website or if you’re developing a website from the ground up.

If you do restructure, then it is necessary to redirect old URLs to a new location in order to maintain at least some link power. Do not get too obsessed with rewriting. If you use underscores instead of dashes, there is no reason whatsoever to migrate to dashes.

An example of URLs rewritten to be static URLs:

http://example.com/cars/acura/TL/

http://example.com/cars/bmw/323.thml

Dynamic parameters are removed and hidden in a static URL. This can be achieved with mod_rewrite. The rewriting process only hides parameters and presents a static structure to the bots. This approach is relatively difficult to implement, as it requires interaction with the database to extract the copy for the URL.

Here are some general rules for rewriting URLs.

  • Spaces should be converted to a slash.

  • Dashes should be selected over underscores. Although there’s little difference, search engines used to treat underscores as a separate character, while dashes were treated as a space. This has changed.

  • Try to maintain a consistent style with all URLs.

Rewriting

When you rewrite PHP URLs you are keeping the actual URL commands intact. For example, PHP script will understand the following and execute the command accordingly:

  • http://example.com/cars/model.php?id=3&product_id=4

but it will give a 404 error for the following URL:

  • http://example.com/cars/acura/TL/

The rewritten URLs don’t actually exist anywhere on the server. The server still has a script which expects to receive commands through the URL string and generate pages depending on those commands. URL rewriting lets you convert PHP URLs to a set of more "natural" links with a static look.

  • Remember that you will need to redirect old URLs to the new location in order to maintain link equity (inbound and outbound) and to prevent 404 errors.

  • You will also need to modify your internal link structure.

The standard for URL rewriting in the Apache world is mod_rewrite, which is why we are using it in this article.

The Apache module mod_rewrite is a killer; it is a really sophisticated module which provides a powerful way to do URL manipulation. With it you can do just about any type of URL manipulation you can think of. The price you have to pay is to accept complexity, because mod_rewrite’s major drawback is that it is not easy for a beginner to understand and use. And even Apache experts sometimes discover new aspects where mod_rewrite can help.

In other words, with mod_rewrite you either shoot yourself in the foot the first time and never use it again, or love it for the rest of your life because of its power.

You do need to know some code to pull this one off.

Here’s some info on mod rewrite:

I assume that you’re using an external host, which means that 99.99% of time mod_rewrite is already installed. If not, then Google will save you. Just look up "installing mod rewrite."

Once you enable mod_rewrite, create a file called .htaccess and put it into the directory of your application. You can also choose to use httpd.conf instead of .htaccess, which has subtle differences. If you choose to go with httpd.conf, then you will need to place rewrite rules inside the <VirtualHost> element of the httpd.conf. Also keep in mind that a slash (/) will be considered to be part of the URL.

On the other hand, .htaccess is perfect if you don’t have access to the configuration file (httpd.conf), you want customized options for a particular directory or want to be able to change options without restarting Apache server. You cannot restart the server if you’re using shared hosting. In this article I assume you’re using .htaccess.

Below you will learn the basic steps of rewriting a URL. This will get you started.

We are going to convert “product.php?product_id=123” into acura-tl-types.html.

The basics: product.php contains all the content. The script receives parameters based on the product_id message and generates output based on that message in the form of a web page. We are going to get rid of parameters and create a search engine and user-friendly URL.

  • Create a folder named “cars.”

  • Create a file named product.php in your “cars” folder.

  • Add the following:

<?php

// display product details

echo ‘This is a test for the product #‘ . $_GET[‘product_id’];

?>

Test your URL http://cars.example.com/product.php?product_id=3

It should display the following:

  • This is a test for the product 3

Go ahead and create a file in the “cars” folder and name it .htaccess. Add the following to it:

RewriteEngine On

# Translate acura-tl-types.html to /product.php?product_id=3

RewriteRule ^ acura-tl-types.html $ /product.php?product_id=3

Go back to the browser one more time and load up http://cars.example.com/acura-tl-types.html. You should get exactly the same page saying:

  • This is a test for the product 3

If you do, then congratulations! You have successfully rewritten your first URL using mod_rewrite! The example is very basic and somewhat impractical, since you need to use it for all URLs on the website. There are more advanced and practical options, which are beyond the scope of this article. Do research on this topic (there are plenty of guides/manuals/articles on the web) and use your search engine optimization knowledge as the base.

More resources for PHP-friendly URLs:

Google+ Comments

Google+ Comments