• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Understanding CI URL Structure

I've read much regarding CI URLs and I feel like I understand the concept of example.com/class/function/ID.

I've also read plenty of posts regarding removing the 'index.php' via the .htaccess file. No real problem there either...I don't think.

What I'm certain I don't understand is this...

I have a controller I've named 'application'. When I load my app, it loads just fine (with or without the 'index.php'). However, when I attempt to access the 'search' function that is part of the application controller class, I get a broken URL.

I've set $route['default_controller'] = 'application' so I can load example.com and see the index.php function in my application controller (this works). I also have a 'search' function in my application controller.

Shouldn't I be able to access the search function as follows: example.com/search ? The only way I've been able to get at that function is as follows example.com/index.php/application/search

Why do I have to reintroduce the 'index.php' into the URL?

Why do I have to add 'application' to the URL? Since I've already set the default controller as 'application' and everything is loaded from there, I would think the search function would be found...but it doesn't.


Sounds to me like your .htaccess file isn't set up properly. It still works without index.php because this is the default file loaded when none is specified. What does your .htaccess file look like?

I'm using the following in my .htaccess file:

DirectoryIndex index.php
<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteBase /affiliate
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php/$1 [L,QSA]
<IfModule !mod_rewrite.c>
  ErrorDocument 404 index.php

When I load my home page, everything is fine. However, when I try to get access to the search function (which is part of the default controller class), I'm forced to use the following URL:


Any idea why I can't just say:


Thanks for the fast response!

The default controller is only used when no url segments are passed. Even when you remove index.php with an htaccess file you still have to pass the url like example.com/controller/method/arg1/arg2/etc.

If your htaccess file is set right you should be able to access that function via example.com/application/search.

To just get example.com/search working you'll need to set a route in application/config/routes.php:

$route['search'] = 'application/search';

okay, so example.com/search doees not work because of the very nature of CI URLs (you pointed it out at the start of your post -> example.com/controller/function/variable).

example.com/search wont work, because CI is looking for a controller named 'search', and if it doesn't find it it throws the error your getting (404). Setting a default controller only allows your to load your base url (www.example.com) without calling the controller name/function name (ie, application -> index). If you want to access a different function within your default controller, you then have to specify the full path.

The correct way to load your 'search' function contained in your 'application' controller is like this:


If you don't like this URL, you can do one of three things...
1. Name a controller called search (www.example.com/search)
2. Use routes to re-route
3. Set up a .htaccess file to make a redirect

Another option, I guess, would be to make your 'application' index funtion just redirect to the search function, but then you still get the same URL as option 1 above. Hope this helps.

Thanks to everyone for their input on this. I'm amazed at how quickly I'm getting responses!

From the most basic level, I'm able to set a default controller and access it at http://www.example.com. What I want to figure out how to create a second controller (not default) and access it. Based on the CI URL structure, I believe a controller named 'search' would be accessible in one of the following two ways:

1. http://www.example.com/search (if 'search' is defined as a controller)

2. http://www.example.com/application/search (if 'search' is defined as a function within the 'application' controller)

Of course, this assumes the .htaccess file is set up properly.

This is correct, right?

Thanks again.

No worries, it's a great community here (but feel free to send someone over to my query about why my pagination is not working - im still waiting! hehe).

Yeah, your two examples above are spot on, either make a new controller called search, or you'll have to provide the full 'contoller/function' path if search is a function inside your application controller.

But consider investigating routes as bubbafoley suggested, they are also handy!

Just wanted to post a follow up after I figured this out in hopes that it will help others.

The .htaccess file I ended up using was created as follows:

RewriteEngine on
RewriteBase /~[USERNAME]/CI/
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L]

The important thing to understand here is I am working on a MacBook. As I am using the generic MacBook setup, my local server is located here:


while the URL structure is:


Furthermore, I have many applications being developed locally so inside my Sites folder, I have a dedicated folder for every application. Let's say that foldername is 'CI' for this example.

If you want to use the .htaccess code above, you will need to replace [USERNAME] with your MacBook username. You will also need to replace CI with your Codeigniter foldername.

I have only tested this locally, but I'm pretty confident it will work after I update the RewriteBase (I won't have my application in a subfolder remotely). With this setup, all I have to do when I move this application to a remote server is update the RewriteBase.

I hope this is helpful to someone. Thanks to everyone for their insight and a special thank you to SOFTKUBE for this helpful post

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.