CodeIgniter URLs

By default, URLs in CodeIgniter are designed to be search-engine and human-friendly. Rather than using the standard“query-string” approach to URLs that is synonymous with dynamic systems, CodeIgniter uses a segment-based approach:

  1. example.com/news/article/my_article

URI Segments

The segments in the URL, in following with the Model-View-Controller approach, usually represent:

  1. example.com/class/method/ID
  • The first segment represents the controller class that should be invoked.
  • The second segment represents the class method that should be called.
  • The third, and any additional segments, represent the ID and any variables that will be passed to the controller.The URI Library and the URL Helper contain functions that make it easyto work with your URI data. In addition, your URLs can be remapped using the URI Routingfeature for more flexibility.

Removing the index.php file

By default, the index.php file will be included in your URLs:

  1. example.com/index.php/news/article/my_article

If your server supports rewriting URLs you can easily remove this file with URL rewriting. This is handled differentlyby different servers, but we will show examples for the two most common web servers here.

Apache Web Server

Apache must have the mod_rewrite extension enabled. If it does, you can use a .htaccess file with some simple rules.Here is an example of such a file, using the “negative” method in which everything is redirected except the specifieditems:

  1. RewriteEngine On
  2. RewriteCond %{REQUEST_FILENAME} !-f
  3. RewriteCond %{REQUEST_FILENAME} !-d
  4. RewriteRule ^(.*)$ index.php/$1 [L]

In this example, any HTTP request other than those for existing directories and existing files are treated as arequest for your index.php file.

Note

These specific rules might not work for all server configurations.

Note

Make sure to also exclude from the above rules any assets that you might need to be accessible from the outside world.

NGINX

Under NGINX, you can define a location block and use the try_files directive to get the same effect as we did withthe above Apache configuration:

  1. location / {
  2. try_files $uri $uri/ /index.php/$args;
  3. }

This will first look for a file or directory matching the URI (constructing the full path to each file from thesettings of the root and alias directives), and then sends the request to the index.php file along with any arguments.