PHP - 使用 pathinfo 变量会使相对路径不起作用


PHP - Using pathinfo variables makes relative paths not to work

我希望自己实现某种CMS。我看到我可以捕获Apache环境变量,称为PATH_INFO,所以这样我就可以在我的网站上制作动态部分(就像joomla一样)。

例如:

stackoverflow.com/index.php/section1/article22

目前,我正在开发一个函数来查找请求的部分和文章,方法是这样做:

$url_seccion = $_SERVER['PATH_INFO'];
$secciones_array =  array_values(array_filter(explode('/', $url_seccion)));

这部分工作正常,我遇到的问题是我现在拥有的所有相对路径都被破坏了。有人可以解释我为什么会发生这种情况以及我该怎么做来解决它吗?(请不要告诉我我必须使用绝对路径...

示例:

$url = "stackoverflow.com/index.php/section1/article22";
$myArray = array_slice( explode('/', $url), 2 );
echo "section: ". $myArray[0] ."<br /> article: ". $myArray[1]."<br />";
if(isset($myArray[0]))  {
$section = $myArray[0];
} else {
$section = "";
}
if(isset($myArray[1]))  {
$article = $myArray[1];
} else {
$article = "";
}
switch(strtolower($section)) {
default:
echo "home";
break;
case 'section1':
echo "function for find and show my article: ". $article;
break;
}

要使用保存在数据库中的部分,您可以使用选择来查找它的 ID,然后使用 文章

若要将所有请求重定向到索引.php请使用:

.htaccess

Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond $0 !^(index'.php|css|js|img)
RewriteRule ^(.*)$ index.php [L]

索引.php:

$url = addslashes($_SERVER['REQUEST_URI']);
$myArray = array_slice( explode('/', $url), 1 );