Welcome Guest, Not a member yet? Register   Sign In
Seo friendly url
#1

(This post was last modified: 04-01-2018, 06:26 AM by cvlancvlan.)

I'm beginner in codeigniter and i try obtain seo friendly url for may project.

This url domain[.]com/blog/category/subcategory/ i want to go here controllers/Blog.php->index()

i want redirect this url domain.com/blog/index/category/subcategory/ to 404

I have this controller

PHP Code:
class Blog extends CI_Controller {
    
    function 
__construct() {
            
        
parent::__construct();
        
    }
    
    function 
index() {
        
        
$config['base_url'] = 'https://domain.com';
        
define('BASE_URL'$config['base_url']);
        
define('SITE_URL'BASE_URL $_SERVER['REQUEST_URI']);    
        
        if ( 
strpos(str_replace('://'''SITE_URL), '//') OR substr(SITE_URL, -1) !== '/' ) {
            
header('Location: ' preg_replace('/([^:\/])(\/+)/','$1/'SITE_URL '/'), TRUE301);
            exit;
        }
        
        
$url trim($_SERVER['REDIRECT_URL'], '/');
        
        
$result $this->db->query('SELECT * FROM blog WHERE url=$url LIMIT 1')->result_array();
        
        if ( 
$result['type'] == 'category' ) {
            
            
$this->_category();
            
        } else {
            
            
$this->_post();
            
        }
        
    }
    
    function 
_category() {
        
        
//load category view
        
    
}
    
    function 
_post() {
        
        
//load post view
        
    
}
    
    function 
ajax_method() {
        
        
//method apelate with ajax
        
    
}
    



I tryed this routes but don't work ( i can't appelate this domain[.]com/blog/ajax_method or other methods from controllers/Blog.php )

Method 1

$route['blog'] = 'blog/index';
$route['blog/.*'] = 'blog/index';

Method 2

$route['blog/(:any)/(:any)'] = 'blog/index/$1/$2'; ( with this method i can domain[.]com/blog/category/subcategory/ but i can't have this url domain[.]com/blog/category/subcategory/filter/brand_dell/color_red/ )

Method 3

PHP Code:
$config['base_url'] = 'https://domain.com';
define('BASE_URL'$config['base_url']);
define('SITE_URL'BASE_URL $_SERVER['REQUEST_URI']);

//route.php
//table routes (id, route_id, sefurl, controller, method)
require_once(BASEPATH 'database/DB.php');
$db =& DB();

if ( 
strpos(str_replace('://'''SITE_URL), '//') OR substr(SITE_URL, -1) !== '/' ) {
    
header('Location: ' preg_replace('/([^:\/])(\/+)/','$1/'SITE_URL '/'), TRUE301);
    exit;
}

$route['default_controller'] = 'blog';
$route['404_override'] = '';

if ( 
count($this->uri->segments) > ) {
    
$sefurl trim($_SERVER['REDIRECT_URL'], '/');
    
$query $db->get_where('routes', ['sefurl' => $sefurl], 1);
    
$result $query->result();
    if ( 
count($result) == ) {
        foreach( 
$result as $row ) {
         
   $route[$row->sefurl] = $row->folder '/' $row->controller '/' $row->method '/' $row->route_id '/';
        }
    } elseif ( 
count($this->uri->segments) > ) {
        
$query $db->get_where('routes', [
            
'folder' => $this->uri->segment(1),
            
'controller' => $this->uri->segment(2
        ], 
1);
        
$result $query->result();
        if ( 
count($result) == ) {
            foreach( 
$result as $row ) {
                
$route[$row->folder '/' $row->controller ''] = 'error404';
                
$route[$row->folder '/' $row->controller '/.*'] = 'error404' 
            
}
        }
    }    
}

$route['admin'] = 'admin/dashboard';
$route['translate_uri_dashes'] = FALSE

This method work but is no pro because
this link domain.com/blog/category/subcategory => blog.php->index() and have this ['config'][uri][rsegments] = array('blog','index')
this link domain.com/blog/ajax_method => blog.php->ajax_method() and have this ['config'][uri][rsegments] = array('blog','index') and here i need ['config'][uri][rsegments] = array('blog','ajax_method') i can change rsegments values in _remap but is no pro Sad

$route['blog'] = 'blog/index';
$route['blog/.*'] = 'blog/index';

PHP Code:
function _remap() {
        if ( 
array_key_exists(2$this->uri->segments) ) {
            
$method $this->uri->segment(2);
//recomanded prefix $method = 'xyz_' . $this->uri->segments(2);
            
if ( method_exists($this$method) ) {
     
           call_user_func([$this$method]);
                return 
false;
     
       }
        }
        
call_user_func([$this'index']);
        return 
false;
 
   
Reply


Messages In This Thread
Seo friendly url - by cvlancvlan - 03-31-2018, 11:23 AM



Theme © iAndrew 2016 - Forum software by © MyBB