开发者

Url rewriting with php? [duplicate]

This question already has an answer here: Simulate file structure with PHP (1 answer) Closed 3 years ago.

Ok, let's say i need to use seo urls. How could i manage the following pattern:

www.mysite.com/{context}/{mode}/

That allows get variables declared as follow:

www.mysite.com/{context}/{mode}/{var}/{val}/{var2}/{val2}...

For example:

www.mysite.com/user/view/id/123

?

Solution?, How to do that?

A possibly solution could be the following: All url are redirected to www.mysite.com/index.php. In the index.php file i divide the url by $u = explode($ulr, '/');: ignoring $u[0] and considering $u[1] as the context, $u[2] as the mode and eventual $u[3], $u[4]... as couples of var-values like get system index.php?var=val. By knowing the context and the mode the right file is included and everybody is happy.

    开发者_开发百科
  • How could i get the url (es: www.mysite.com/user/view/id/123) or just the last part (es: user/view/id/123)?
  • Is this system has any cons?


The solution i did used is:

.htaccess

<IfModule mod_rewrite.c>
    RewriteEngine On
    #Rewrite the URI if there is no file or folder
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

url_manager (only for root files: www.mysite.com/index.php; to use it inside a folder (www.mysite.php/folder/index.php) add +1 to every numeric key and to $i)

function urlFile()
{
    $url = explode('/', $_SERVER["REQUEST_URI"]);
    $context = $url[1];
    $mode = $url[2];
    return "{$context}_{$mode}.php";
}

function get($string)
{
    $url = explode('/', $_SERVER["REQUEST_URI"]);
    unset($url[0], $url[1], $url[2]);

    $i = 3;
    foreach ($url as $u)
    {
        $get[$url[$i]] = $url[$i + 1];
    }

    return $get[$string];
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜