使用seo目的的路由隐藏或删除url中的控制器名称= codeigniter

时间:2015-01-05 06:40:12

标签: php .htaccess codeigniter

我正在使用一个控制器。如何使用路由删除或隐藏URL上的控制器名称? 我已经设置了我的.htaccess来删除我的index.php。我已尝试过其他类似问题的大量代码,但我没有运气。我不知道如果我的实时服务器出现问题,因为在我之前的项目中我使用了这段代码

$route['del/(:any)'] = "my_cotroller";
$route['(:any)'] = 'pages/view/$1';

这完全没问题但是当我尝试在我当前的项目中使用它时它没有用。 这是我的默认路线

$route['default_controller'] = "my_cotroller";
$route['404_override'] = '';

我想归档像{{​​3}}这样的网址,但我当前的路线代码为我提供了http://www.sample.com/login

感谢您的帮助。

2 个答案:

答案 0 :(得分:4)

使用CI时的简单方法:

示例控制器:

<?php
class Haha extends CI_Controller
{
    public function check()
    {           
        $this->load->view('path/to/check/view');
    }

    public function choke()
    {
        $this->load->view('path/to/choke/view');
    }
}

示例路线:

//same name with C method
$route['check'] = 'haha/check';
$route['choke/(:any)'] = 'haha/choke';

//or custom route name
$route['squirrel'] = 'haha/check';
$route['woohoo/(:any)'] = 'haha/choke';

我的htaccess:

RewriteEngine on
RewriteCond $1 !^(index\.php|assets|images|js|css|uploads|favicon.png)
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^(.*)$ ./index.php/$1 [L]

访问uri:

http://yoursite.com/check
http://yoursite.com/choke/whatever

//or

http://yoursite.com/squirrel
http://yoursite.com/woohoo/whatever

增加: 这是我使用CI的旧项目的完整路线,也许你可以得到一些东西。

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

$route['default_controller'] = 'sitehome';

$route['page'] = 'sitehome/page';
$route['page/who-we-are'] = 'site/whoweare';
$route['page/why-use-us'] = 'site/whyus';
$route['page/about-us'] = 'site/about';
$route['page/contact-us'] = 'site/contact';
$route['page/news'] = 'site/news/news';
$route['page/topics'] = 'site/topic';
$route['page/polls'] = 'site/polls';
$route['page/results'] = 'site/result';
$route['agent/detail/(:any)'] = 'site/agentdetail';
$route['register'] = 'site/register';
$route['login'] = 'site/login';

$route['user/home'] = 'usermain';
$route['user/edit-account'] = 'user/editaccount';
$route['user/retire'] = 'user/retired';
$route['user/business/data'] = 'user/business';
$route['user/business/topic/create'] = 'user/topic/topiccreate';
$route['user/business/topic/view'] = 'user/topic/topicview';
$route['user/business/topic/edit/(:num)'] = 'user/topic/topicedit';
$route['user/inbox'] = 'user/inbox';
$route['user/message/read/(:any)'] = 'user/readmessage';
$route['user/provider/list'] = 'user/providerlist';
$route['user/provider/send-message/(:any)'] = 'user/providerlist/sendmessage';
$route['user/notification'] = 'user/notified';
$route['user/inquiry'] = 'user/inquiry';

//testing purpose
$route['percobaan'] = 'coba/percobaan';

$route['poll'] = 'site/polls/retrievePoll';

//serve response
$route['response/single-poll'] = 'api/ResponseData/serveHomePoll';
$route['response/multi-poll'] = 'api/ResponseData/servePoll';

$route['404_override'] = 'notFound';
$route['translate_uri_dashes'] = FALSE;

/* End of file routes.php */
/* Location: ./application/config/routes.php */

注意:

  

http://example.com/business/将失败或不存在。

祝你好运。

答案 1 :(得分:0)

您可以在config / routes.php中定义自定义路由 - 例如:

$route['about'] = 'name_controller/about';

然后,http://example.com/about 转到http://example.com/name_controller/about

查看更多http://ellislab.com/codeigniter/user-guide/general/routing.html