1

我有一个网站,它的 URL 结构对面包屑完全没有用,有利于 SEO,或者对用户来说很直观。有点像

asdf.com/directory/listing/{unique_id}/{unique-page-name}/

我真的很想将其更改为

asdf.com/{state}/{city}/{unique_id}/{unique-page-name}/

或非常相似的东西。这样,我可以实现面包屑的形式

Home > State > City > Company

有人对将当前结构转换为我上面描述的结构有任何想法吗?无论我怎么看,似乎都需要对网站进行彻底的检修。能够向用户展示类似的东西会很棒Home > Florida > Miami > Bob's Haircuts

谢谢!

4

1 回答 1

2

你只需要对你的路线有创意: http: //ellislab.com/codeigniter/user-guide/general/routing.html

您可以设置一条路线来捕获所有流量并将其指向directory/listing,然后在您的listing方法中 - 您可以手动访问 url 段。例如:

// application/config/routes.php
$route[':any'] = "directory/listing";
    /**
     you might have to play with this a bit, 
     I'm not sure, but you might need to do something like:
       $route[':any'] = "directory/listing";
       $route[':any/:any'] = "directory/listing";
       $route[':any/:any/:any'] = "directory/listing";
       $route[':any/:any/:any/:any'] = "directory/listing";
    */

// application/controllers/directory.php

function listing()
{
    // docs: http://ellislab.com/codeigniter/user-guide/libraries/uri.html
    $state = $this->uri->segment(1);
    $city = $this->uri->segment(2);
    $unique_id = $this->uri->segment(3);
    $unique_page_name = $this->uri->segment(4);

    // then use these as needed

}

或者,可能是这种情况,您需要能够调用其他控制器和方法 -

您可以更改 URL 以指向控制器,然后执行列表操作 -

所以你的网址会变成:

asdf.com/directory/{state}/{city}/{unique_id}/{unique-page-name}/

你的路线会变成:

 $route['directory/:any'] = "directory/listing";

然后,您需要更新listing方法中的 uri 段以匹配第 2、3、4 和 5 个段。

这样,您仍然可以调用另一个控制器,并且它不会被您的自定义路由捕获:

asdf.com/contact/  --> would still access the contact controller and index method

更新

您还可以发挥创意并使用正则表达式在第一个 uri 段中捕获具有状态名称的任何 url - 然后将它们推送到directory/listing,然后所有其他控制器仍然可以工作,您不必directory在 url 中添加控制器。像这样的东西可能会起作用:

// application/config/routes.php
$route['REGEX-OF-STATE-NAMES'] = "directory/listing";
$route['REGEX-OF-STATE-NAMES/:any'] = "directory/listing"; // if needed
$route['REGEX-OF-STATE-NAMES/:any/:any'] = "directory/listing"; // if needed
$route['REGEX-OF-STATE-NAMES/:any/:any/:any'] = "directory/listing"; // if needed


/**
REGEX-OF-STATE-NAMES -- here's one of state abbreviations:
    http://regexlib.com/REDetails.aspx?regexp_id=471
*/
于 2013-01-15T18:08:42.593 回答