Codeigniter SEO Friendly URL Structure - Mostlikers

13 April, 2016

Codeigniter SEO Friendly URL Structure

URL structure is one of the most important technique to improve SEO. A good URL structure can help in increasing the website traffic volume. There are many websites on the internet that do not have a proper URL structure, especially in case of shopping carts.

Codeigniter SEO Friendly URL Structure




In this post we will be seeing, how to create a proper SEO friendly URL structure using Codeiginter.
It is easy to structure the URL for search engines and human readability as Codeigniter designs the SEO friendly URL without any queries. 

Removing index.php from url

By default the codeigniter URL structure will be www.yourwebsite.com/index.php/any_other_page/. You can notice the index.php in the URL which is not user friendly, you can easily remove index.php by using a .htaccess code.

Create a new file in the root folder of  Codeigniter with file name as".htaccess".

             

Paste the below code in the .htaccess file.

RewriteEngine on
RewriteCond $1! ^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

Now type the URL without the index.php and the URL work's just fine. If it doesn't work then make sure that the value of the  $config['index_page'] is empty in the file config.php in the folder /application/config.

$config['index_page'] = ""

Default controller

When u download the Codeigniter framework the default controller will be welcome controller. To change the default controller, open your application/config/routes.php file and make the below change.

$route['default_controller'] = 'welcome'
change to 
$route['default_controller'] = 'other_controller'

URI Routing 

URI rountimg is the major function in the Codeiginter because it will work with dynamic url, with the help of URI rounting you can write clean url structure. 

Example :

example.com/welcome/about
example.com/welcome/service
example.com/welcome/contact

While looking welcome/about doe's not have more SEO friendly. Remove the welcome example.com/about it will be looking fine. Follow the below steps to clean the urls.


Setting Rounting urls

Routing rules are defined in your application/config/routes.php file. you can see the default_controller class name to this line follow the rules to define the URL structure.

$route['default_controller'] = "welcome";
$route['about'] = "welcome/about";
$route['service'] = "welcome/service";
$route['contact'] = "welcome/contact";

Now remove the controller class name example.com/about, it will work.


Setting Dynamic URL's Route

A Dynamic URL containing the words and numbers. Routing developed with wildcard You can match literal values or you can use two wildcard types.

(:num) -  will match a segment containing only numbers.
(:any)   - will match a segment containing any character. 

Example :

example.com/blog/index/1
example.com/blog/index/12
example.com/product/index/2
example.com/news/index/3


Incase that id value is static write text with the controller class name
$route['blog/new-topic-list'] = "blog/index/1";
$route['blog/old-topic-list'] = "blog/index/12";


Dynamic Product URL

Store your product URL's to the database pass the specify product url along with the controller class name.

Example category url

example.com/mens-clothing/t-shirt/
example.com/mens-clothing/shirt/
example.com/mens-clothing/trousers/

rounting code would be

$route['mens-clothing/(:any)']    =   "mens/clothing/$1";

mens - It is a controller class name
mens/clothing/ -It is a controller function name
$1- it is a third url segment value.

Example product urls

example.com/mens-fit-shirt-sale/
example.com/woman-fit-shirt-sale/
example.com/kids-fit-shirt-sale/

rounding code should be

$route['default_controller'] = "product";
$route['(:any)'] = "product/index/$1";

Adding a URL Suffix

Adding a URL Suffix at the end of the your url is a good thing, suffixes like .html, .php. With these extenstion search engines also consider the page as static .

Open file /application/config/config.ph, find the $config['url_suffix'] line. Add your extension format and save the file

$config['url_suffix'] = '.html';

example.com/product/men-shirts it will work with example.com/product/men-shirts.html









Related Topics

4 comments: