2011-10-09 135 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);