【问题标题】:How to replace underscores in codeigniter url with dashes?如何用破折号替换codeigniter url中的下划线?
【发布时间】:2011-12-24 12:47:37
【问题描述】:

出于 seo 的原因,我想知道将我的 codeigniter url 的下划线更改为破折号的最简单的解决方案。

我的控制器看起来像:

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

所以要浏览到这个页面,我必须去:

www.domain.com/request_guide

但我希望对 seo 更友好,并使用破折号而不是下划线,如下所示:

www.domain.com/request-guide

我的印象是 codeigniter 函数需要下划线(可能是错误的)。

在以前的项目中,我只是简单地使用了 mod_rewrite,但我相信这些操作可以使用路由来执行。

对我来说,用破折号替换下划线的最简单方法是什么???

【问题讨论】:

  • request-guide 并不比 request_guide 对 seo 更友好。
  • 如果您考虑一下,那将匹配一个类名,其中破折号无效;实际上,您需要将类(或方法)名称写为“request-guide”,这是无效的 php 语法。按照以下答案中的建议进行重新路由
  • Google 给我的印象是,他们将破折号归类为空格和包含下划线的单词作为一个完整的单词。
  • @Ianzz request-guide 比 request_guide 对 seo 更友好。连字符在 URL 中对 SEO 非常友好,Google 将它们解释为单词分隔符。

标签: php codeigniter url-rewriting


【解决方案1】:

这真的取决于你的意图。如果您只想更改一页,那么 devrooms 的解决方案确实是完美的:

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

但是如果你想让它成为你网站的默认行为,你应该像这样扩展你的核心路由器类(来源: [Using hyphens instead of underscores in CodeIgniter]

  1. 在“application/core”中创建一个新文件并将其命名为“MY_Router.php”
  2. 在其中插入此代码:

    <?php
    
    defined('BASEPATH') || exit('No direct script access allowed');
    
    class MY_Router extends CI_Router {
    
        function _set_request ($seg = array())
        {
            // The str_replace() below goes through all our segments
            // and replaces the hyphens with underscores making it
            // possible to use hyphens in controllers, folder names and
            // function names
            parent::_set_request(str_replace('-', '_', $seg));
        }
    
    }
    
    ?>
    

更新(2015 年 10 月 26 日):正如@Thomas Wood 所提到的,CodeIgniter 3 中有一种更好的方法:

$route['translate_uri_dashes'] = TRUE;

【讨论】:

  • 这似乎不起作用。如上所述创建文件,但没有任何反应。有什么想法吗?
  • 我以为他们在 PHP6 中强制关闭 PHP 标签?
  • @christopher-thomas 感谢您的建议!我认为这个解决方案写的太多了,不能只用一行代码就可以完成。
  • 我建议的链接的好处是,它仅适用于类和方法,因此它不会触及 url 的其他部分,例如,如果您在末尾有参数使用 - 在其中,它们也会被转换。这种方式只转换需要的部分,所以即使多几行,实际上也更有针对性,整体也更完整
  • 是否应该设置为 TRUE?
【解决方案2】:

中找到的路由配置
config/routes.php

是你的朋友。

一个简单的

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

会为你做这件事。

【讨论】:

    【解决方案3】:

    Code Ignitor 3 内置了这个:

    $route['translate_uri_dashes'] = FALSE;

    只需更改为TRUE,即可使用_-

    Documentation

    【讨论】:

    • 很高兴知道。谢谢!
    • 我不确定,但它仍然存在于documentation(右下角)
    • 如果您在application/controllers/my_folder/About_us.php 之类的目录或文件夹中有控制器,这似乎不起作用
    【解决方案4】:

    打开 application/config/routes.php 并更改

    $route['translate_uri_dashes'] = TRUE;
    

    现在当你访问www.domain.com/**request-guide**时,它会实例化request_guide控制器。

    它适用于名称包含 _(下划线)的所有控制器。

    【讨论】:

      【解决方案5】:

      看看 Codeigniter 的自定义路由http://codeigniter.com/user_guide/general/routing.html

      【讨论】:

        【解决方案6】:
        $route['request-guide'] = "request_guide";
        

        【讨论】:

          【解决方案7】:

          您可以做的是创建一个自定义挂钩(PST...您需要基本的 CodeIgniter 技能): 有关 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']);
          }
          

          我将文件命名为 customhooks.php

          然后将这个添加到 application/config 中的 hooks.php 文件中:

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

          您需要编辑 application/config/config.php 文件以启用挂钩

          $config['enable_hooks'] = TRUE;
          

          额外:

          这样当您使用 $this->uri->uri_string() 时,它会保持连字符,请执行以下操作 Creating Core System Classes

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

          【讨论】:

            【解决方案8】:

            您可以使用此 _remap() 方法来处理此类行为。将此方法放在您的控制器中或创建一个核心控制器并将其放入。

            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();
            }
            

            【讨论】:

              猜你喜欢
              • 2013-03-22
              • 2010-11-19
              • 2013-04-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-05-07
              • 2010-09-12
              • 1970-01-01
              相关资源
              最近更新 更多