Codeigniter 3 - how to remove function name from url
My url:
example.com/controller/function/parameter => example.com/category/index/category_name
I need:
example.com/category/category_name
I tried several solutions provided by the Stackoverflow questions asked on this but it doesn't work. Either it redirects to the home page, or 404 page not found
.
The options I've tried:
$route['category'] = "category/index"; //1
$route['category/(:any)'] = "category/index"; //2
$route['category/(:any)'] = "category/index/$1"; //3
Another route:
$route['default_controller'] = 'home';
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 the config file I have:
$config['url_suffix'] = '';
source to share
I think you have a bug in your file .htaccess
. The code is .htaccess
shown below.
You can use RewriteBase
to provide a base for rewriting.
RewriteEngine On
RewriteBase /campnew/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]
In the controller, your method.
public function index($category_name = null) {
$this->load->model('category_model');
$data = array();
if ($query = $this->category_model->get_records_view($category_name)) {
$data['recordss'] = $query;
}
if ($query2 = $this->category_model->get_records_view2($category_name))
{
$data['recordsc2'] = $query2;
}
$data['main_content'] = 'category';
$this->load->view('includes/template', $data);
}
In the model file
public function get_records_view($category){
$this->db->where('a.linkname', $category);
}
Let me know if it doesn't work.
source to share
I'm not sure why you weren't able to get it to work.
Here is some test code I created to test it ... This uses CI 3.1.5.
.htaccess is the same as yours ...
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;
Check URLs
/category/
output: The Category name is None Selected
/category/fluffy-bunnies
output: The Category name is fluffy-bunnies
Play around with this and see if you can find the problem.
source to share