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

↘锁芯ラ 提交于 2019-11-28 07:32:35

问题


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

回答1:


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";



回答2:


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';



回答3:


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/




回答4:


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

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




回答5:


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 :)




回答6:


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

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




回答7:


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.



来源:https://stackoverflow.com/questions/12863095/how-to-remove-controller-name-from-url-making-it-clean-in-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!