How to remove controller name from url, making it clean in codeigniter

I have the following url ..

http://localhost/ci/site_controller/home 

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

  http://localhost/ci/home 

How to do it in CodeIgniter?

Note. If you ask what I tried, than I just did a Google search, since I don't know how to use mod_rewrite.

EDIT

I have it on my .php routes

 $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 
+7
source share
7 answers

You can specify a route for each URL:

In the config / routes.php file, simply set each page as follows:

 $route['ci/home'] = "ci/site_controller/home"; 
+10
source

This can help you determine the default controller for your CodeIgniter project.

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

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

 $route['default_controller'] = 'site_controller'; 
+3
source

Assuming you have your url http://localhost/ci/site_controller/home , you can simply write an explicit route, as shown below, in /application/config/routes.php

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

or

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

if your controller has names marked as /application/controllers/ci/

+2
source
+1
source

Drop 'ci' from your routes. This is the name of your project and is never required. Routes always begin at the controller level:

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

Will work. However, more importantly ... are you sure your design is right? Why not just let home be the controller? Create /application/controllers/home.php and you can access index() using http://localhost/ci/home .

You complicate things. No need for routes.

0
source

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

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

Hope this helps you :)

0
source

It helps me. In " routes.php " I added the following:

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

-one
source

All Articles