【问题标题】:Hyperlink multi-level sub-directories through index.php? structure通过index.php超链接多级子目录?结构体
【发布时间】:2015-08-16 17:27:38
【问题描述】:

我正在尝试创建一个可以访问多级子文件夹的超链接结构。现在,我只能通过一级 (php) 目录访问我的超链接,例如 index.php?content=about(而 'about' 是 about.php)。

我想做的是为具有多级子目录的大型网站创建一个文件系统,如下例所示, index.php?blog/category/process/。我不知道是否有一个符号可以替换 html 符号 / (目录的斜线)但是 PHP 目录。我尝试了不同的方式来访问多级子目录中的文件,例如将 ? (问号)。这一切都是假设性的实验。如果我使用斜杠,例如 'blog/category/process/filename.php' 形式,我会收到 404 Not Found 错误。

function.php 中有以下 PHP 脚本,它从 URL 获取内容,如果没有内容,它会设置一个默认值,如果有内容,它会清理数据以防止黑客攻击:

function loadContent($where, $default='') {
$content = filter_input(INPUT_GET, $where, FILTER_SANITIZE_STRING);
$default = filter_var($default, FILTER_SANITIZE_STRING);
$content = (empty($content)) ? $default : $content;

if ($content) {
$html = include 'content/'.$content.'.php';
return $html;
  }
}

function loadIncludes($where, $default='includes/') {

$includes = filter_input(INPUT_GET, $where, FILTER_SANITIZE_STRING);
$default = filter_var($default, FILTER_SANITIZE_STRING);

if($includes) {

$html = include 'includes/'.$includes.'.php';
    return $html;
  }
}

“blog/category/process/filename.php”内的文档链接显示(之前是“404错误”,但标题和css文件不起作用。它们没有被拾取。

请注意:
我的网站结构是
标头(index.php 拾取一个标头?)
内容(多个内容=filename.php)
页脚

index.php 看起来像这样:

<?php   
require ('includes/function.php');
require ('includes/init.php');  /* init.php picks up the header */
?>

<div class="clearboth"></div>

<!-- ********  HOMEPAGE  **********  -->
<?php loadContent('content', 'home'); ?>

<div class="clearboth"></div>

<!-- ********  FOOTER  **********  -->  
<?php include ('content/footer.php');  ?>

我通过index.php找到了多级子目录结构的解决方案?也拾取了CSS,如下:

<a href="index.php?content=blog/category/process/filename">
(leave out the extension .php followed after 'filename')

感谢您的帮助!

【问题讨论】:

  • 您使用什么服务器端技术?阿帕奇?
  • 1.你能编辑 .htaccess 规则吗? .htaccess 中的实际重写规则是什么。
  • @eLearner #RewriteCond %{HTTPS_HOST} ^mywebsite\.com$ [OR] #RewriteCond %{HTTPS_HOST} ^www\.mywebsite\.com$ #RewriteRule ^(.*) $ "https\:\/\/mywebsite\.com\/$1" [R=301,L] --- 我不介意在这里分享我的网站地址,但我不确定是否会违反规则。感谢您调查我的问题。

标签: php html hyperlink


【解决方案1】:
<?php
/* FOLDERS STRUCTURE

$_SERVER['DOCUMENT_ROOT'] = C:/Server/www/music; // YES Windows :)
THIS FILE = folders.php

C:/Server/www/music
C:/Server/www/music/files/
C:/Server/www/music/files/folder1/
C:/Server/www/music/files/folder1/folder2/myfile.jpeg

*/

$root = $_SERVER['DOCUMENT_ROOT'];
$requestURI = $_SERVER['REQUEST_URI'];
$startFolder = '/files/';
preg_match('/\?/', $requestURI) ? list($requestURI, $fileToFind) = explode('?', $_SERVER['REQUEST_URI']) : exit('Bad request'); 
$file = $root.$startFolder.$fileToFind;    

if(file_exists($file)){

    //YOUR CODE 
}
else {

    header("HTTP/1.0 404 Not Found");

}

// Usage = http://music.com/folders.php?/folder1/folder2/myfile.jpeg
?>

阅读我之前提出的那些问题。希望对你有帮助

.htaccess : Redirect all http requests to one file whitout 404 resp. code

Use PHP to rewrite URLS?

请注意,如果文件存在或不存在,您可以使用 http 标头 header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");header($_SERVER["SERVER_PROTOCOL"]." 200 Ok");

发挥你的想象力...

【讨论】:

  • 这听起来可能很愚蠢,但我将您的脚本复制/粘贴为 .php 文件并执行了“require_once”。结果,我的主页消失了。标题中的导航菜单在 index.php 文件中可见。链接有效,但多级目录结构仍然无效。我怀疑,我可能误解了我的问题。我需要的是 // 用法 = music.com/index.php?folder1/folder2/myfile.PHP。目前,它只是一个一级导航系统。多级目录结构仅适用于 CSS 文件,例如../img/thumbnails/mypicture.jpg。不像 PHP 文件中的导航。
  • 感谢您编译上述脚本,感谢您的时间和知识。我已经修改了上面的原始问题。它可能对下一步有所帮助。
  • 对不起! (节假日)您找到解决问题的方法了吗?
  • 还没有。感谢您跟进。 :)
  • @e-Learner,非常感谢您一直以来的帮助。您对“使用 PHP 重写 URLS”的指导是一个很好的引导!我从那里调查并找到了我一直在寻找的答案。我希望这也可以帮助我在谷歌上搜索过类似问题的其他人,但他们严格保留了一级子目录结构。有人建议使用“爆炸”,但我发现上述解决方案要简单得多。再次感谢。
【解决方案2】:

我通过index.php找到了多级子目录的超链接结构的解决方法?也就是拾取了 CSS,并且不会发出 404 错误,如下:

<a href="index.php?content=blog/category/process/filename">
descriptive header
</a>

(注意:记得省略'filename'后面的扩展名.php)

【讨论】:

  • 当然。如果$content 是“blog/category/process/filename.php”,您认为'content/'.$content.'.php' 会产生什么?是的,它显然是“content/blog/category/process/filename.php.php”。如果您甚至没有进行基本调试,您将无法编写任何代码。只需打印变量 $html 的内容就足够了,您会看到。
  • @Dawid,我觉得有误会。我的问题不是添加 .php 或忽略它。我试图访问多级子文件夹中的文件。将扩展名 .php 添加到文件名末尾时,我从来没有遇到过问题。该注释仅用于提醒您不要使用 .php 扩展名。再看看我的回答,它说 ...content=blog/category/process/filename... =(等号)在这里很重要,并且仍然为其余文件夹保留正斜杠。跨度>
  • 我明白了。但是您从一开始就知道正确的 URL 是什么:index.php?content=about。您所说的“多级子文件夹”并没有什么特别之处。 URL 还是一样的:index.php?content=how/many/sub/levels/you/want。无论如何,我很高兴你自己解决了这个问题。
  • @Dawid,我不知道如何解决它。这是一本书学习的策略,没有显示多级示例,因为内容本身就是一个文件夹,猜测其余部分将是斜线是一个疯狂的猜测。我从 =$1& 和许多其他角色猜测开始。只是对我来说没有意义,为什么相等,然后是斜线。但是谢谢你... :)
  • 这很简单。参见例如Wikipedia。问号后面的部分 URL 称为 query。格式为parameter1=value&amp;parameter2=value...。您的参数是content(在 PHP 脚本中使用),它的值可以是任何值。您需要将其设置为路径。在 Linux 中,路径由斜杠分隔(与 Windows 上的反斜杠相反)。因此你需要content=path/to/file。网络服务器的整个index.php?content=whatever 表示:执行index.php 并将content 参数设置为“whatever”。 PHP 脚本包含 content 参数中的内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-15
  • 1970-01-01
  • 2016-02-04
  • 1970-01-01
  • 2015-10-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多