开发者

url parse - route not found

开发者 https://www.devze.com 2023-04-11 14:49 出处:网络
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.

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消