2011-10-09 128 views
0

我设计了一个网站,它工作正常,但最近我修改了SEO目的的链接。我刚刚用-替换了_。现在我得到一个路由未找到错误。url解析 - 路由未找到

这是匹配阵列

$routes = array(
    array('url' => '/^products\/(?P<cat>\w+)$/', 'controller' => 'products', 'view' => 'products_list') 
); 

链接是这样的

http://localhost/product/sample-page

当我删除-_它的工作原理更换。

+2

我敢打赌'\ w'不匹配 “ - ”。 – Pointy

+0

\ w用于匹配数字,单词字符(字母,数字和下划线)和空格(空格,制表符和换行符)。 –

+0

@JeremySpouken AFAIK,'\ w'不_匹配空格,制表符或换行符。 – Shef

回答

1

将简写字符类\w(它将字母,数字和下划线匹配)更改为更明确的正则表达式,以匹配大小写字母,数字_-

$routes = array(
    array('url' => '/^products\/(?P<cat>[A-Za-z0-9_-]+)$/', 'controller' => 'products', 'view' => 'products_list') 
); 
+1

非常感谢,它的工作。 –

0

你也可以离开\ W在那里,但加 - 明确,即

$routes = array(
array('url' => '/^products\/(?P<cat>[\w\-]+)$/', 'controller' => 'products', 'view' => 'products_list') 

);

+0

谢谢你工作时我从\ \ \ \ \ –

0

你可以在这里查看一个例子:

http://rubular.com/r/czUkSgbYjs

你还可以用不同的正则表达式玩。

+0

\ \ \ \ \中删除\,谢谢这个链接是非常有用的。 –

0

嗨朋友,我发现了另一种解析url的方法。使用这个你可以在搜索引擎友好的url结尾处加上“.html”。

.htaccess文件

RewriteEngine On 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-l 

RewriteRule ^(.+)$ index.php?url=$1 [QSA,L] 

URL解析代码

$url = isset($_GET['url']) ? $_GET['url'] : null; 
    $url = str_replace(".html", "", $url); 
    $url = str_replace("-", "_", $url); 
     //you could use rtrim(); but I had some trouble when the url had the "t" as the ending character. 
    //$url = rtrim($url, '.html'); 
    $url = explode('/', $url);