内部重写+使用 mod-rewrite/.htaccess 设置环境变量

发布于 2024-09-30 01:32:24 字数 1478 浏览 0 评论 0原文

我试图通过mod_rewrite实现以下目标:

1.基本目录结构是什么?

  • /
    • 主/(文件夹)
      • 一堆 PHP 文件
    • some_folder/(文件夹)
    • another_folder/(文件夹)
    • ...(任意数量的此类文件夹)
    • yet_another_folder/(文件夹)
    • .htaccess(魔法就在这里)

2. 我想要做什么做?

在这种情况下,所有可执行文件都包含在主文件夹中,并且只能通过其他文件夹访问它们。此外,PHP 脚本依赖 apache 环境变量向它们传递一些信息,这些信息与访问它们的文件夹的名称相关。另一个特殊要求是主文件夹不应直接访问(只能通过内部重定向)。

3.我走了多远?

我实现了以下 .htaccess 文件:
<代码> 重写引擎开启
RewriteCond %{REQUEST_URI} !^/(main|another)/.*$
RewriteRule ^(.*)/(.*)$ /main/$2 [QSA,L,env=VAR_NAME:$1/weird_include.php]
它通常会进行重定向,但是存在一些问题...

4. 有什么大不了的?

使用这种方法,仍然可以直接访问main(以及另一个)文件夹,并且我希望只能通过其中一个辅助文件夹来访问它。 第二个问题是,由于存在重写,mod_rewrite 向变量添加了 REDIRECT_ 前缀,并且由于我完全无法控制驻留在 main 中的源代码,并且它需要特定的变量名称,这对我来说根本不起作用。 另外,我还没有找到一种合适的方法来隔离对 mainother-main 文件夹的访问...

5. 我认为帐篷的钥匙在哪里?

PHP 文档中有关该主题的这篇文章建议添加类似于以下内容的规则这个
RewriteRule ^index\.php$ \ - [E=VAR1:%{REDIRECT_VAR1},E=VAR2:%{REDIRECT_VAR2},L]
会解决这个问题。但是我不知道如何修改它以符合“禁止直接访问”的要求。还有 - mod_rewrite 引入了哪些开销? 另一个重要的问题是如何以上述方式限制对 mainother-main 的访问?
将在这两个文件夹中添加一个 .htaccess 文件,该文件会检查 REDIRECT_VAR_NAME1,如果存在,则将其重写为 VAR_NAME,保持 url 不变,如果没有重定向到 403禁止 则执行以下操作诡计?如果有任何关于此开销的提示,我们将不胜感激。

I am trying to achieve the following through mod_rewrite:

1. What is the basic directory structure?

  • /
    • main/ (folder)
      • a bunch of PHP files
    • some_folder/ (folder)
    • another_folder/ (folder)
    • ... (an arbitrary number of such folders)
    • yet_another_folder/ (folder)
    • .htaccess(the magic goes here)

2. What I am trying to do?

In this scenario all the executable files are contained in the main folder and they should be accessed only through the additional folders. Moreover, the PHP scripts rely on an apache environment variable to pass them some information, which is related to the name of the folder through which they are accessed. Another special requirement is that the main folder should not be accessible directly (only through an internal redirect).

3. How far did I get?

I implemented the following .htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(main|another)/.*$
RewriteRule ^(.*)/(.*)$ /main/$2 [QSA,L,env=VAR_NAME:$1/weird_include.php]

It generally does the redirect, however there are some issues...

4. What is the big deal?

Using this approach, the main (and the anotheras well) folder can still be accessed directly, and I want it to be accessible only through one of the auxiliary folders.
The second problem is that, because there is a rewrite, the mod_rewrite adds a REDIRECT_ prefix to the variables and since I have completely no control over the source code residing in main and it requires specific variable names, that doesn't work for me at all.
Also I haven't found a decent way yet to isolate the access to main and other-main folders...

5. Where I believe the key to the tent is?

This post in the PHP doc on the topic suggests adding a rule similar to this one
RewriteRule ^index\.php$ \ - [E=VAR1:%{REDIRECT_VAR1},E=VAR2:%{REDIRECT_VAR2},L]
would resolve the issue. However I don't see how I can modify that to conform to the 'no direct access' requirement. And also - what overhead does mod_rewrite introduce?
The other important question would be how to restrict the access to main and other-main in the forementioned way?
Would adding a .htaccess file in these two folders, that does checks for REDIRECT_VAR_NAME1 and if it is present, rewrites it to VAR_NAME, leaving the url intact, and if it is not redirects to 403 forbidden do the trick? And also any hint towards what the overhead of this would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七度光 2024-10-07 01:32:24

如果 /main/ 中的脚本绝对要求环境变量具有特定名称,并且您无法更改环境变量,请使用 php.ini 中的 auto_prepend_file 指令在每个 PHP 脚本的开头。这将允许您预处理环境变量,而无需实际修改任何其他文件。

例如,如果您想要 FOO 但您无法使用 REDIRECT_FOO,请将以下行放入您指定为 auto_prepend 的任何 PHP 文件中。这会将 REDIRECT_FOO 重新分配给 FOO。事后执行的脚本不会知道发生了什么。

$_ENV['FOO'] = $_ENV['REDIRECT_FOO'];

关于你的另一个问题,是的,重写这样的所有内容会增加一点开销,但我不确定会增加多少。与 PHP 脚本所做的任何事情相比,它可能可以忽略不计。

要防止直接访问 /main/,请将以下内容放入 .htaccess 文件中。

<Location /main/>
    Order deny,allow
    Deny from all
</Location>

顺便说一句,如果您对脚本本身有任何控制权,请修复它们,而不是依赖这样的黑客。

If the scripts in /main/ absolutely require that environmental variable have particular names, and if you can't change the environment variables, use the auto_prepend_file directive in php.ini to include a particular PHP file at the beginning of every PHP script. This would allow you to pre-process environment variables without actually modifying any other file.

For example, if you want FOO but you're stuck with REDIRECT_FOO, put the following line in whatever PHP file you've designated as auto_prepend. This will reassign REDIRECT_FOO to FOO. The script that gets executed afterwords wouldn't have a clue what happened.

$_ENV['FOO'] = $_ENV['REDIRECT_FOO'];

On your other question, yes, rewriting everything like that adds a bit of overhead, but I'm not sure how much. It's probably negligible compared to whatever your PHP script is doing.

To prevent direct access to /main/, put the following in your .htaccess file.

<Location /main/>
    Order deny,allow
    Deny from all
</Location>

BTW, if you have any control over the scripts themselves, fix them instead of relying on hacks like this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文