3

我有一个使用这种风格的网站:/index.php?page=45&info=whatever&anotherparam=2

我计划有漂亮的 url 将以前的 url 转换为:/profile/whatever/2

我知道我必须使用 .htAccess 并将所有内容重定向到 index.php。没关系。

我的问题更多在 index.php (前端控制器)中。如何重建$_GET["info"]$_GET["anotherparam"]能够继续使用$_GET[...]其页面中使用的所有现有代码?

$_GET[...]我是否必须使用一些代码在标题中重新构建 GET,或者我是否必须通过创建自己的数组来摆脱每个页面上的所有内容,该数组将永远解析/并分配类似 :$myParam["info"] = "whatever"而不是在页面中使用$myParam[]而不是$_GET[]

我不想修改所有那些使用$_GET[]

编辑:

我的 .htAccess 看起来像:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ index.php [NC,L]

不存在的所有内容都转到 index.php。因为我已经使用了这个结构: /index.php?page=45&info=whatever&anotherparam=2没有任何东西被破坏。但是现在我将使用/profile/whatever/2并且在 switch 情况下我可以确定要访问哪个页面,include(..)但问题在于所有 GET 参数。如何构建它们以使用 $_GET[] 从所有页面访问?

4

2 回答 2

3
$path = ... // wherever you get the path $_SERVER[...], etc.
            // eg: /profile/wathever

$segments = split ($path);

$segments_name = Array ('page', 'info', 'anotherparam');
for($i=0;$i  < count ($segments); $i++) {
  $_GET[$segments_name[$i]] = $segments[$i];
}

使用此解决方案,您必须始终在同一位置使用相同的参数

如果您不希望有两个解决方案: - 使用 /page/profile/info/wathever 之类的路径 - 使用路由器系统(为此我建议您使用框架而不是手动完成所有操作)

编辑:第二种解决方案

$path = ... // wherever you get the path $_SERVER[...], etc.
            // eg: /page/profile/info/wathever
$segments = split ($path);

for($i=0;$i  < count ($segments); $i+=2) {
  $_GET[$segments[$i]] = $segments[$i+1];
}
于 2010-03-02T21:00:01.113 回答
0

请改用 switch 语句。还记得修改你的 .htaccess

<?php
switch ($_GET) {
    case "home":
     header('Location: /home/');
        break;
    case "customer":
        header('Location: /customer/');
        break;
    case "profile":
        header('Location: /profile/');
        break;
}
?>
于 2010-03-02T21:23:50.020 回答