CodeIgniter Routes

CodeIgniter Routes is used for making URLs SEO-friendly. It plays a vital role in the project to make a clean URL for digital marketing. We can also achieve this by using .htaccess rules, but CodeIgniter Routes is easy. CodeIgniter Routes can be easily used as compared to .htaccess rules.

CodeIgniter routes file is located in application>config>routes.php.

In CodeIgniter; URL is made by the following sequence index.php/controller/function name. Let’s see how routes work:

Example:  In this example, we will create an about.php file in view, and the default URL will be like this

index.php/Welcome/about
By using routes we will make this URL like
/about-web-development-institute
 


Step1:
Create a file in the view folder named about.php

<!DOCTYPE html>
<html>
<head>
<title>About Us</title>
</head>
    <body> <h1>About us</h1> </body> 
</html>


Step2:
Create about function in Welcome Controller

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
 
class Welcome extends CI_Controller {
public function index() 
{
  $this->load->view('index');
}
 public function about() 
{
  $this->load->view('about');
}
 
}


Step3:
Open the routes.php file and write the following code:

$route['about-web-development-institute'] = 'welcome/about';

 

By using these three steps, you can make your URL clean for SEO purposes. But this route rule is only used for static pages.

Now we will see if the page is dynamic what routes rule will be:

There are two main wildcards  to make a clean URL for dynamic pages:

1. :num: this rule is used when you have a number in the URL.

Example: localhost/blog/web-development-blogs/5

$route['category/(:any)/(:num)']='welcome/category/$1/$2';

Here the first segment of URL is category, the second segment can be anything, and the third segment will be number.

 

2.:any: this rule is used when we want an alphanumeric URL. Any means you can use numeric and alphabetic both.

Example: localhost/blog/ file-upload-in-php-mysql

$route['blog/(:any)']='welcome/blogdetail/$1';

Here the first segment of URL is ‘blog’ and the second segment as anything means the number or alphabetical.

 

Url Suffix

URL suffix is used to change the suffix of URL. It is fun to use means you can make your URL suffix whatever you want. By Default, in CodeIgniter URL does not contain any suffix, but sometimes clients have demand to use the .html suffix in the URL. .html suffix mostly seen in Magento cms.

Where to change and in which file:

Step1:  Open config.php file, which is located in application>config>config.php

Step2:  find this $config['url_suffix'] = '';

Step3:  Add suffix whatever your want

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

Categories: web development codeigniter

Trending Courses

CodeIgniter

Regular : 45 Days

Fastrack : 20 Days

Crash : 10 Days

Advance Digital Marketing

Regular : 6 Months

Fastrack : 3 Months

Crash : 2 Months

React JS

Regular : 45 Days

Fastrack : 20 Days

Crash : 7 Days

Laravel

Regular : 45 Days

Fastrack : 20 Days

Crash : 10 Days

Front End Developer

Regular : 7 Month

Fastrack : 4 Month

Crash : 2 Month

Related Blogs

Request For Demo