我设计了一个网站,它工作正常,但最近我修改了SEO目的的链接。我刚刚用-
替换了_
。现在我得到一个路由未找到错误。url解析 - 路由未找到
这是匹配阵列
$routes = array(
array('url' => '/^products\/(?P<cat>\w+)$/', 'controller' => 'products', 'view' => 'products_list')
);
链接是这样的
当我删除-
或_
它的工作原理更换。
我设计了一个网站,它工作正常,但最近我修改了SEO目的的链接。我刚刚用-
替换了_
。现在我得到一个路由未找到错误。url解析 - 路由未找到
这是匹配阵列
$routes = array(
array('url' => '/^products\/(?P<cat>\w+)$/', 'controller' => 'products', 'view' => 'products_list')
);
链接是这样的
当我删除-
或_
它的工作原理更换。
将简写字符类\w
(它将字母,数字和下划线匹配)更改为更明确的正则表达式,以匹配大小写字母,数字_
和-
。
$routes = array(
array('url' => '/^products\/(?P<cat>[A-Za-z0-9_-]+)$/', 'controller' => 'products', 'view' => 'products_list')
);
非常感谢,它的工作。 –
你也可以离开\ W在那里,但加 - 明确,即
$routes = array(
array('url' => '/^products\/(?P<cat>[\w\-]+)$/', 'controller' => 'products', 'view' => 'products_list')
);
谢谢你工作时我从\ \ \ \ \ –
嗨朋友,我发现了另一种解析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);
我敢打赌'\ w'不匹配 “ - ”。 – Pointy
\ w用于匹配数字,单词字符(字母,数字和下划线)和空格(空格,制表符和换行符)。 –
@JeremySpouken AFAIK,'\ w'不_匹配空格,制表符或换行符。 – Shef