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

后端 未结 9 1294
余生分开走
余生分开走 2020-12-20 16:50

I have the following url..

http://localhost/ci/site_controller/home

I want to remove site_controller controller

相关标签:
9条回答
  • 2020-12-20 16:58

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

    0 讨论(0)
  • 2020-12-20 17:01

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

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

    0 讨论(0)
  • 2020-12-20 17:07

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

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

    0 讨论(0)
  • 2020-12-20 17:12

    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";
    
    0 讨论(0)
  • 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';
    
    0 讨论(0)
  • 2020-12-20 17:16

    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%.

    0 讨论(0)
提交回复
热议问题