3

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'] = '';
2
  • $route['category/(:any)'] = "category/index/$1"; //3 this must work fine. I have tried it. And getting correct response Commented Jul 9, 2017 at 5:29
  • i think you just need route #3. can you show your category controller? Commented Jul 9, 2017 at 5:30

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

localhost/campnew/category/garden-tents-2380 takes to home page... While link localhost/campnew/category/index/garden-tents-2380 displays "The Category name is garden-tents-2380 " on top of the page
@TimBrownlaw Default routes should be first ones but also when ever you have some kind of wild card placeholder, it should go at most end line applicable.
@Tpojka ideally yes. I just put it at the top to make it stick out a bit more and to ensure it got first go when testing... Remember it is just Debug/test code...
I can (or you too) notice it on first look. I am just telling that proposed code should be given in a way like there is no chance for producing other eventual errors (not explicitly nor implicitly) tied to first problem.
0

I think you have error in your .htaccess file. Please find below code for .htaccess file.

You can use RewriteBase to provide a base for your rewrites.

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 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 Model File

public function get_records_view($category){
    $this->db->where('a.linkname', $category); 
}

Let me know if it not works.

8 Comments

it still redirects to the home page
Have you added $route['category/(:any)'] = "category/index/$1"; line in your config/routs.php file.?
Also how you are passing parameters in your model function..? I can not see it in your controller function.
In model function using where('a.linkname', $this->uri->segment(3)); to get the related details from db & show products in view-page coming under that category
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.