How To Customise URLs and Create SEO-Friendly URLs in CodeIgniter

How To Customise URLs and Create SEO-Friendly URLs in CodeIgniter

CodeIgniter, the PHP framework, allows for URL customization so that the URLs are more friendly to search engines. As a rule of thumb when you deal with URLs, it is best to avoid long URLs. To shorten your CodeIgniter URLs, for e.g. changing the default controller/method format in the URLs to just reflect the method name.

As an example, if we had an online product catalogue and had a URL format of http://www.yourdomain.com/catalogue/list/shoes/412, we can actually create a route, so that users can just enter a more SEO-friendly URL of: http://www.yourdomain.com/list/shoes/412

To do this, open up your routes.php file, i.e. at /system/application/config/routes.php, and define a new route in CodeIgniter, as such:

$route['list/:any/:num'] = ‘catalog/list/$1/$2′;

Here’s a breakdown of the route:

:any can contain any characters
:num contains only numerals
$1 is the first parameter, which is the product type, in this case, shoes (defined as a :any type)
$2 is the second parameter, which is the product number, in this case, 412 (defined as a :num type)

Note that routes will run in the order that they appear in the routes file, so routes that appear higher will always take precedence over lower routes.

, , , , , , , , ,

About Site Fixit!

Sam is a professional web designer and web developer. He has over 15 years of experience with web-related technologies, and loves making things work.

View all posts by Site Fixit!

No comments yet.

Leave a Reply