8

I have the following url..

http://localhost/ci/site_controller/home

I want to remove site_controller controller from url resulting in..

 http://localhost/ci/home

How can I do this in CodeIgniter ?

Note: If you'll ask what I've tried than I've just done searching over Google as I don't know how to use mod_rewrite.

EDIT

I have this in my routes.php

$route['default_controller'] = "site_controller/home";
$route['ci/home'] = 'ci/site_controller/home';
$route['404_override'] = '';

but still not working!

.htaccess

RewriteEngine On
RewriteBase /ci/
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]

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

ErrorDocument 404 /index.php
Yousuf Memon
  • 4,638
  • 12
  • 41
  • 57

9 Answers9

10

You can set your a route for each url:

In your config/routes.php file, just set each page like this:

$route['ci/home'] = "ci/site_controller/home";
Catfish
  • 18,876
  • 54
  • 209
  • 353
3

This might help you to define a Default Controller for your CodeIgniter project.

https://codeigniter.com/user_guide/general/controllers.html#defining-a-default-controller

For instance, in your case, open your application/config/routes.php file and set this variable:

$route['default_controller'] = 'site_controller';
A J
  • 3,970
  • 14
  • 38
  • 53
2

assuming you currently have your url http://localhost/ci/site_controller/home you can just write an explicit route like below to your /application/config/routes.php

$route['ci/home'] = 'site_controller/home';

or

$route['ci/home'] = 'ci/site_controller/home';

if your controller is namespaced to /application/controllers/ci/

skilleo
  • 2,451
  • 1
  • 27
  • 34
2

This helped for me. In "routes.php" I added this:

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

Eduardo Russo
  • 4,121
  • 2
  • 22
  • 38
Lokanath
  • 31
  • 2
1

try using the routes file to re-map url's

http://ellislab.com/codeigniter/user_guide/general/routing.html

Kumar V
  • 8,810
  • 9
  • 39
  • 58
Millard
  • 1,157
  • 1
  • 9
  • 19
0

I just found the solution in this link, it works for me: http://ellislab.com/forums/viewthread/148531/

$route['^(page1|page2|page3|page4)(/:any)?$'] = "YOURCONTROLLERNAME/$0";

Hope it helps you :)

ric
  • 541
  • 4
  • 7
0

In the case that your .htaccess file is set up like this (or similar), so index.php is already removed...

RewriteEngine On
RewriteCond $1 !^(index\.php|application|assets|images|js|css|uploads|favicon.png)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

And your codeigniter code lies directly in the html folder...

To remove the controller name from URL, edit application/config/routes.php Add lines like these:

$route['signin'] = 'web/signin';

The line above calls function signin() of controller Web when user requests yoursebsite.com/signin

To pass variables into said function, do the following:

$route['p/(:any)'] = 'web/p/$1';

This line calls function p() in controller Web, which has a parameter. Function p() was defined as:

public function p($param = "none") {
    echo $param;
}

I hope this helps :)

K.Naumov
  • 13
  • 1
  • 5
0

It's so simple if you want to remove the controller name from URL in the Codeigniter 3.x in my case URL was: http://localhost:8080/index.php/page/sometext Where "Page" is the controller name open application/config/routs.php

$urlParam = $this->uri->segment_array()[1];
$rout[$urlParam] = "page/index";

Result: http://localhost:8080/index.php/sometext

I assure you, it will work. tested 100%.

Rashid Khan
  • 328
  • 3
  • 12
-1

Drop the 'ci' from your routes. That is your project name and is never required. Routes always start on the controller level:

$route['home'] = "site_controller/home";

Will work. However, more importantly.. are you sure your design is correct? Why not just let home be a controller? Create a /application/controllers/home.php and you'll be able to access it's index() function with http://localhost/ci/home.

You're complicating things. No need for routes at all.

Jordan Arsenault
  • 7,100
  • 8
  • 53
  • 96
  • And then any pages that run off that controller will be /ci/home/whatever. Or do you propose creating a controller for every page? – Rick Calder Oct 13 '12 at 04:20
  • The way I create all my apps - and the way CodeIgniter docs suggest is to do exactly this. Utilize controllers for pages and when you need to interact with that page (POST data for example) use a separate function in that page's controller. `controllers/about.php`,`controllers/home.php`,`controllers/contact.php`, etc - is a valid design. – Jordan Arsenault Oct 13 '12 at 04:23
  • Fair enough, I prefer to group like pages in one controller, but our site has quite a few pages so it makes sense to me to do it my way. To each their own. – Rick Calder Oct 13 '12 at 04:28
  • it really is just a matter of design preference. Either *will* work. It's up to you to decide whether you want to let your controller names be the entry point and avoid writing routes - or - to let your function names be your entry points and write routes. A suggest the former, but it's your call. The latter method can get hairy when you want to start setting permissions on your controllers. The constructor function can often be used for this in the former but doesn't lend itself nicely to the way you proposed. – Jordan Arsenault Oct 13 '12 at 04:31
  • I have separate base controllers for that, MY_Controller, User_controller, Admin_controller, then I just handle the individual roles within those main groups with regular controllers – Rick Calder Oct 13 '12 at 04:45