How to replace underscores in codeigniter url with

2019-03-08 04:19发布

I would like to know the simplest solution to changing the underscores of my codeigniter urls to dashes, for seo reasons.

My controllers look like:

public function request_guide() {
...Load view etc...
}

So to browse to this page I would have to go to:

www.domain.com/request_guide

But I want to be more seo friendly and use dashes instead of underscores, like so:

www.domain.com/request-guide

I am under the impression that codeigniter functions require underscores (might be wrong).

In previous projects I have simply used mod_rewrite but I believe that these actions can be performed using routes.

What is the easiest way for me to rewrite the urls replacing the underscores with dashes???

8条回答
Deceive 欺骗
2楼-- · 2019-03-08 04:42
$route['request-guide'] = "request_guide";
查看更多
甜甜的少女心
3楼-- · 2019-03-08 04:45

What you could do is create a custom hook (PST... you need basic CodeIgniter skills): for more information regarding CodeIgniter Hooks - Extending the Framework Core

/*
 * the hooks must be enabled from the config file
 * replace underscore with dashes (hyphens) for SEO
 */

function prettyurls() {
    if (is_array($_GET) && count($_GET) == 1 && trim(key($_GET), '/') != '') {
        $newkey = str_replace('-', '_', key($_GET));
        $_GET[$newkey] = $_GET[key($_GET)];
        unset($_GET[key($_GET)]);
    }
    if (isset($_SERVER['PATH_INFO']))
        $_SERVER['PATH_INFO'] = str_replace('-', '_', $_SERVER['PATH_INFO']);
    if (isset($_SERVER['QUERY_STRING']))
        $_SERVER['QUERY_STRING'] = str_replace('-', '_', $_SERVER['QUERY_STRING']);
    if (isset($_SERVER['ORIG_PATH_INFO']))
        $_SERVER['ORIG_PATH_INFO'] = str_replace('-', '_', $_SERVER['ORIG_PATH_INFO']);
    if (isset($_SERVER['REQUEST_URI']))
        $_SERVER['REQUEST_URI'] = str_replace('-', '_', $_SERVER['REQUEST_URI']);
}

I named the file customhooks.php.

Then add this to the hooks.php file in application/config:

$hook['pre_system'] = array(
    'class' => '',
    'function' => 'prettyurls',
    'filename' => 'customhooks.php',
    'filepath' => 'hooks',
    'params' => array()
);

You will need to edit your application/config/config.php file to enable hooks

$config['enable_hooks'] = TRUE;

EXTRA:

so that when you use $this->uri->uri_string() it stays hyphenated do the following Creating Core System Classes

class MY_URI extends CI_URI {

    function uri_string() {
        return str_replace('_', '-', $this->uri_string);
    }

}
查看更多
闹够了就滚
4楼-- · 2019-03-08 04:47

Code Ignitor 3 has this in built:

$route['translate_uri_dashes'] = FALSE;

Just change to TRUE and you can use either _ or -.

Documentation

查看更多
Summer. ? 凉城
5楼-- · 2019-03-08 04:47

You can use this _remap() method to handle such behavior. Place this method in your controllers or create a core controller and place it in.

public function _remap($method, $params = array()){
    if(method_exists($this, $method)){
        return call_user_func_array(array($this, $method), $params);
    }else{
        $method = str_replace("-", "_", $method);
        if(method_exists($this, $method)){
            return call_user_func_array(array($this, $method), $params);
        }
    }
    show_404();
}
查看更多
萌系小妹纸
6楼-- · 2019-03-08 04:48

Open application/config/routes.php and change

$route['translate_uri_dashes'] = TRUE;

That is it you need to do.

Now when you access www.domain.com/request-guide, it will instantiate request_guide controller.

It will work with all controllers with name containing _ (underscore).

查看更多
该账号已被封号
7楼-- · 2019-03-08 04:50

The routes config found in

config/routes.php

is your friend here.

A simple

$route['request-guide'] = "request_guide" ;

will do this for you.

查看更多
登录 后发表回答