I have designed a website, it was working fine, but lately I modified the links for SEO purposes. I just replaced _
with -
. Now I am getting a route not found error.
This is the matching array
$routes = array(
array('url' => '/^products\/(?P<cat>\w+)$/', 'controller' => 'products', 'view' => 'products_list')
);
The li开发者_StackOverflownk goes like this
http://localhost/product/sample-page
When I remove -
or replace it with _
it works.
Change your regex from the shorthand character class \w
, which matches letters, digits, and underscore, to a more explicit one to match upper and lower case letters, digits, _
, and -
.
$routes = array(
array('url' => '/^products\/(?P<cat>[A-Za-z0-9_-]+)$/', 'controller' => 'products', 'view' => 'products_list')
);
You can check here an example:
http://rubular.com/r/czUkSgbYjs
And you can play with different regex.
You could also leave the \w in there, but add - explicitly, i.e.
$routes = array(
array('url' => '/^products\/(?P<cat>[\w\-]+)$/', 'controller' => 'products', 'view' => 'products_list')
);
Hi friends I found another method to parse url. Using this you could have ".html" at the end of url for Search engine friendly.
.htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
url parse code
$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);
精彩评论