【问题标题】:How can I eliminate the controller name in the url via routes in = codeigniter如何通过= codeigniter中的路由消除url中的控制器名称
【发布时间】:2015-01-06 07:10:57
【问题描述】:

这是Hiding or removing controller name in url using routes for seo purpose = codeigniter我第一个问题的后续问题

我需要在 url 中隐藏或删除控制器名称。所以我遵循了 Nucleo 1985 给我的答案,这对于静态页面非常有效。我知道我的问题有些不同,所以我得到了不同的解决方案。

我正在使用一个控制器。

我的控制器中有一个功能,上面有一个开关盒。每个案例都包含 url。示例(http://www.sample.com/my_controller/my_function/my_case_url)。 /my_case_url 是动态的。

我为每个函数创建了单独的路由,这很麻烦,不适用于我的具有 switch case url 的函数。

我的问题是。

如何获得像http://www.sample.com/my_function/http://www.sample.com/my_function/my_case_url/ 这样的网址? (点击链接重定向到页面时必须去掉或隐藏函数名)

我需要这个用于 SEO 目的。

谢谢!

【问题讨论】:

    标签: php .htaccess codeigniter routes


    【解决方案1】:

    这将与您的第一个问题相同

    // it will go to my_controller index    
    $route['my_function'] = 'my_controller'; 
    
    // you can set specific controller method remove method will go to index.
    $route['chomy_functione/(:any)'] = 'my_controller/my_function'; 
    

    【讨论】:

      【解决方案2】:

      这应该从project_name/application/config/routes.php这个文件中完成。添加一行

      $route['url_first/url_second'] = "any_controller_name/function_name";
      

      【讨论】:

        【解决方案3】:

        您可以在控制器中添加方法 _remap()

        https://ellislab.com/codeigniter/user-guide/general/controllers.html#remapping

        将以下代码放入您的控制器中

        /**
         * Intercept all calls to this class. 
         * 
         * @access private
         * @param string
         * @param array
         * @return boolean
         */
        function _remap($method, $params)
        {
            // If method exists, call that method.
            if (method_exists($this, $method) !== false) return call_user_func_array(array($this, $method), $params);
        
            // If method is actually an existing permalink, show permalink's content
            if ($this->SomeModel->exists(array('permalink' => $method))) return $this->view($method);
        
            // Non-existing method
            show_404();
        }
        

        【讨论】:

          猜你喜欢
          • 2011-09-05
          • 1970-01-01
          • 2013-09-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-11-01
          相关资源
          最近更新 更多