可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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:
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 :)
回答8:
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.