我設計了一個網站,它工作正常,但最近我修改了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