【问题标题】:htaccess rewriting to include file in every folderhtaccess 重写以在每个文件夹中包含文件
【发布时间】:2014-05-16 06:07:37
【问题描述】:

有没有一种方法可以让我使用 htaccess 的 url 重写功能看起来像有一个文件,比如说 /public/some/folder,而实际上它在 /public 中。

例如,我在根文件夹中有 index.php,我希望它在我访问 index.php 时也像它在所有文件夹和子文件夹中的行为一样

当我执行 index.php 时,它也会在所有文件夹和子文件夹中执行

更新

请通过下面的例子理解我的问题

index.php 在根目录下,我在根目录下也有不同的文件夹 所以当我通过浏览器访问 index.php 时,它也会在其他文件夹中执行

http://example.com/index.php 也将表现得好像它也在子文件夹中一样

http://example.com/folder1/index.php

http://example.com/folder2/index.php

http://example.com/folder3/index.php

index.php 不在这些文件夹中,但它也必须同时在这些文件夹中执行

我觉得通过这个例子不难理解,请相应回答

【问题讨论】:

  • 你的意思是像 index.php?page=category&subcat=tet 更改为 example.com/category/test ?
  • @Soundz NO 不会更改,但 index.php 应该表现得好像它也在其他文件夹中一样,而无需手动将 index.php 上传到每个文件夹我只想在主文件夹中拥有 index.php 并且想要同时在其他文件夹中执行它
  • 换句话说,您想阻止对包含图像/文件的文件夹编制索引?

标签: php .htaccess mod-rewrite url-rewriting rewrite


【解决方案1】:

我不确定我是否理解正确。但是任何方式你都可以在你的 .htaccess 中尝试类似的东西

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]

因此,在它之后,所有内容都将使用 $_GET[page] 参数转到 index.php。因此,例如 www.mydomen.com/try/to/get/this 将转到我们的 index.php,其中我们将有我们的 $_GET[page]="try/to/get/this"。如果您将对此字符串使用explode,您可以为您的网址编写任何您想要的规则。类似于您自己的 URL 管理器 :)

示例:

将 index.php 和 .htaccess 放在根文件夹中 在你的 index.php 中:

$urlArray = explode("/",$_GET['page']);
switch($urlArray[0]){
  case "try":
     include($_SERVER[DOCUMENT_ROOT]."/path/to/folder/what/you/need/my.php");
     break;
  case "another_url":
     include($_SERVER[DOCUMENT_ROOT]."/another/path/to/folder/what/you/need/my_another.php");
     break;
}

希望这是你需要的。

【讨论】:

  • 这不是我需要的。我通过一个例子更新了我的问题,请阅读并相应回答
  • 我不确定这是否是您需要的方式。你能描述你的问题吗?你想达到什么目标?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-17
  • 1970-01-01
  • 2018-09-04
  • 2015-09-27
  • 2011-09-13
相关资源
最近更新 更多