SEO url gives 404-error in CodeIgniter

614 Views Asked by At

I am pretty new to codeigniter. I do know php.

How can I accomplish to load the right view?

My url: /blog/this-is-my-title

I’ve told the controller something like

if end($this->uri->segment_array()) does exist in DB then load this data into some view.

I am getting an 404-error everytime I access /blog/whatever

What am i seeing wrong?

3

There are 3 best solutions below

0
On BEST ANSWER

unless you're using routing, the url /blog/this-is-my-title will always 404 because CI is looking for a method called this-is-my-title, which of course doesn't exist.

A quick fix is to put your post display code in to another function and edit the URLs to access posts from say: /blog/view/the-post-title

A route like:

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

may also achieve what you want, if you want the URI to stay as just `/blog/this-is-my-title'

0
On

The may be more possibilities:

  1. The most common - mod_rewrite is not active
  2. .htaccess is not configured correctly (if u didn't edited it try /blog/index.php/whatever)
  3. The controller does not exist or is placed in the wrong folder

    Suggestion: if you only need to change data use another view in the same controller

    if (something) {

    $this->load->view('whatever');

    }

    else

    { $this->load->view('somethingelse'); }

    If none of those works post a sample of code and configuration of .htaccess and I'll take a look.

0
On

The best way to solve this problem is to remap the controller. That way, you can still use the same controller to do other things too.

No routing required!

enter code here
<?php
class Blog extends Controller {
function __construct()
{
    parent::__construct();
}
public function _remap($method, $params = array())
{
    if (method_exists($this, $method)) 
    {
        $this->$method();
    }
    else 
    {
        $this->show_post();
    }
}
function index()
{
    // show blog front page
    echo 'blog';
}
function edit()
{
    // edit blog entry
}
function category()
{
    // list entries for this category
}
function show_post()
{
    $url_title = $this->uri->segment(2);
    // get the post by the url_title
    if(NO RESULTS)
    {
        show_404();
    }
    else
    {
        // show post
    }
}
  }
 ?>