解析SEO友好网址没有htaccess或mod_rewrite

时间:2013-11-19 15:56:35

标签: php url seo

任何人都可以在php或函数中建议一个方法来解析不涉及htaccess或mod_rewrite的SEO友好网址吗?例子很棒。

http://url.org/file.php/test/test2#3

返回:Array(scheme)=> http [host] => url.org [path] => /file.php/test/test2 [fragment] => 3)/file.php/测试/ TEST2

我如何将/file.php/test/test2部分分开?我猜test和test2会是参数。

编辑:

@Martijn - 在收到有关您答案的通知之前,我确实弄明白了您的建议。谢谢顺便说一下。这被认为是一种好方法吗?

$url = 'http://url.org/file.php/arg1/arg2#3';
$test = parse_url($url);
echo "host: $test[host] <br>";
echo "path: $test[path] <br>";
echo "frag: $test[fragment] <br>";
$path = explode("/", trim($test[path]));
echo "1: $path[1] <br>";
echo "2: $path[2] <br>";
echo "3: $path[3] <br>";
echo "4: $path[4] <br>";

1 个答案:

答案 0 :(得分:2)

您可以使用explode从阵列中获取部件:

$path = trim($array['path'], "/"); // trim the path of slashes
$path = explode("/", $path);
unset($path[0]); // the first one is the file, the others are sections of the url

如果你真的想让它再次成为zerobased,请将其添加为最后一行:

$patch = array_values($path);

回复您的修改:
您希望尽可能灵活,因此不需要基于最多5项的固定编码。虽然你可能永远不会超过它,但是不要把自己固定在它上面,只是你不需要的开销。

如果你有这样的页面系统:

id parent  name                url
1   -1      Foo                 foo
2    1      Bar, child of Foo   bar-child-of-foo

制作递归函数。将数组传递给一个函数,该函数使第一部分找到根项

SELECT * FROM pages WHERE parent=-1 AND url=$path[0]

该查询将返回一个id,在父列中使用该数据的下一个值。取消设置$ path数组的每个找到的值。最后,您将拥有一个包含其余部分的数组。

草拟示例:

function GetFullPath(&$path, $parent=-1){
    $path = "/"; // start with a slash
    // Make the query for childs of this item
    $result = mysqli_query($conn, "SELECT * FROM pages WHERE parent=".$parent." AND url=".current($path)." LIMIT 1");
    // If any rows exists, append more of the url via recursiveness:
    if($result->num_rows!==0){
        // Remove the first part so if we go one deeper we start with the next value
        $path = array_slice($patch,1); // remove first value
        $fetch = $result->fetch_assoc();
        // Use the fetched value to go deeper, find a child with the current item as parent
        $path.= GetFullPath($path, $fetch['parent']);
    }
    // Return the result. if nothing is found at all, the result will be "/", probs home
    return $path;
}

echo GetFullPath($path); // I pass it by reference, any alterations in the function happen to the variable outside the scope aswell

这是一个草稿,我没有对此进行测试,但你得到的想法是我试图草绘。您可以使用相同的方法获取您所在页面的ID。只是继续将变量重新传回c

其中一天我得到了递归的悬念^^ 再次编辑:哎呀,结果证明是一些代码。