2013-02-15 49 views
0

目前我的url看起来像mysite.com/product/display/10意味着domain/controller/function/id为SEO友好的缘故我想附加product title这将像product name something如何使用Kohana生成SEO友好的URL

与我的SEO基本知识我知道什么是,对于SEO友好我url应该像mysite.com/product/display/product-name-something/id

是否有任何有效的方式Kohana 2.x做到这一点,如果它是不可能的Kohana,请建议一PHP使用.htaccess文件或不使用.htaccess有效和更好的方法。

回答

1

你应该使用ROUTES来声明友好的URL。在文件的config/routes.php文件,像这样:

$config['route'] = 'class/method'; 
你的情况

$config['product/display/([a-zA-Z0-9-]+)/([0-9]+)'] = 'product/display/$2'; 

更多:http://docs.kohanaphp.com/general/routing

要改变字符串中使用这样的:

function toUrl($string) { 
     // small fonts 
     $sText = strtolower($string); 
     // change spaces to - 
     $sText = str_replace(' ', '-', $sText); 
     // delete all other characters to - 
     $sText = preg_replace('|[^0-9a-z\-\/+]|', '', $sText); 
     // delete too much - if near 
     $sText = preg_replace('/[\-]+/', '-', $sText); 
     // trim - 
     $sText = trim($sText, '-'); 

     return $sText; 
} 
+0

感谢你的回应,在这里我需要将'产品名称'转换为'产品名称东西',我怎么能这样做呢?计算好。 – 2013-02-15 12:11:51

+0

请在这个'$ config ['product/display /([a-zA-Z0-9 - ] +)/([0-9] +)'] ='product/display中为第二个参数建议正则表达式模式/ $ 2'这样''它必须是可选的',它将是'product-name-something'这样的单词的组合。 – 2013-02-15 13:17:16

+0

$ config ['product/display /([a-zA-Z0-9 - ] +)(/([a-zA-Z0-9 - ] +))?'] ='product/display/$ 2';如果有东西是可选的,就把它放在(表达式)中? – Boogeroos 2013-03-28 13:52:22