Ecommerce Developer
 
 

Code

SEO-Friendly URLs in CodeIgniter

 

CodeIgniter, the PHP framework, allows for URL customization so that the URLs are more visible to the search engines. By default, CodeIgniter has disabled the GET method to ensure security of the system and provide a custom link structure. A URL in CodeIgniter looks like this:

www.mywebsite.tld/controller/method/param1/param2/../paramN

"Controller" in the URL is the name of the Controller class that we want to access. All controllers can be found by default in system/application/controllers.

"Method" in the URL is the name of the function that corresponds to our page and param1, param2, …, paramN are the variables that we send to the page.

We can have URLs like www.mywebsite.tld/catalog/product/3215. However these URLs don't help us very much with search engine optimization (SEO). So we will probably want something like www.mywebsite.tld/catalog/product/best-laptop/3215 or even www.mywebsite.tld/best-laptop/Toshiba-3215.

In the first case, catalog is the controller, product is the name of the method, and best-laptop is param1. The name of the product and 3215 is the product ID, used to make the selection from the database. I would consider this to be a better URL because the product name is in our URL as well as a category that is more positive than just product.

However, we should take our URL rewrite a step further. It is best to avoid long URLs; so in the second case, www.mywebsite.tld/best-laptop/toshiba-3215 is a bit better. Here we don't use the controller/method format anymore, we use just the function name. To be able to do this we have to go to system/application/config/routes.php and define a new route in CodeIgniter.

Routes in CodeIgniter

In CodeIgniter, a route can be defined in two ways: wildcards or regular expressions.

For the wildcard method, our rerouted URL would look like this:

		$route['product/:any/:num'] = 'catalog/product/$1/$2';

The portion of the route described as :any contains any characters and :num contains only numeric characters. $1 corresponds to param1, as defined as type :any and $2 corresponds to param2, defined as :num. Param1 is the name of the product and param2 is the product ID.

The second method uses regular expressions and can be defined like this:

	$route['product/([a-z]+)/(\d+)'] = "catalog/product/$1/$2";

Param1 is defined using the regular expression [a-z]+, which describes a range of lowercase characters to be searched for. The plus sign tells the parsing engine to look for one or more characters. Without the plus sign, the regular expression would seek just one lowercase letter. Param2 is defined as numeric (\d+).

You can use both wildcards and regular expressions in creating a route, if you wish. Routes will run in the order they are defined. Higher routes will always take precedence over lower ones, so be sure you understand the order in which you are defining routes.

Removing the Product ID from the URL

Let's say that we want our URL to be like product/best-laptop, without the product ID part.

There are several ways to do this. However, I would suggest adding a new field in the Admin → Add product page, i.e.; adding a permalink. This must be unique for each product (make sure you set up some form of validation for product names). Once the name is selected, it will represent the param1 part of the URL. Using this method, we can keep the product name different from what appears in the URL, which gives us even more power to customize (rewrite) our URLs for SEO purposes.

Related Articles

2 Comments

Rss-sm