Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter 3 - how to remove the function name from URL

My URL is:

example.com/controller/function/parameter
=> example.com/category/index/category_name

I need:

example.com/category/category_name

I have tried several solutions provided by Stackoverflow questions asked on this, but it´s not working. Either it redirects to home or a 404 page not found.

The options I have tried are:

$route['category'] = "category/index"; //1

$route['category/(:any)'] = "category/index"; //2

$route['category/(:any)'] = "category/index/$1";  //3

Another route is:

$route['default_controller'] = 'home';

The htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]

In config file I have:

$config['url_suffix'] = '';
like image 872
Chetan Deora Avatar asked Jul 09 '26 12:07

Chetan Deora


1 Answers

I am not sure why you couldn't get it to work.

Here is some test code I created to check this out... This is using CI 3.1.5.

.htaccess - same as what you have...

Controller - Category.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Category extends CI_Controller {

    public function __construct() {
        parent::__construct();
    }

    public function index($category_name = 'None Selected') {
        echo "The Category name is " . $category_name;
    }
}

routes.php

$route['category/(:any)'] = "category/index/$1";  //3 - this works

$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

Test URLS

/category/ output: The Category name is None Selected

/category/fluffy-bunnies output: The Category name is fluffy-bunnies

Have a play with that and see if you can find the issue.

like image 180
TimBrownlaw Avatar answered Jul 11 '26 02:07

TimBrownlaw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!