简体   繁体   English

PHP $ _SERVER ['DOCUMENT_ROOT']和htaccess?

[英]PHP $_SERVER['DOCUMENT_ROOT'] and htaccess?

I know that you cannot simply set DocumentRoot in htaccess but it's all I have access to. 我知道您不能简单地在htaccess中设置DocumentRoot,但这就是我可以访问的全部。 Is there a work around? 有没有解决的办法? How should I proceed? 我应该如何进行? Or is this the wrong configuration for a setup like this? 还是这样的设置错误的配置?

I have a dev server where: www.foo.com is mapped to /usr/www/users/foo. 我有一个开发服务器,其中:www.foo.com映射到/ usr / www / users / foo。

I then have 然后我有

  • www.foo.com/peter => /usr/www/users/foo/peter www.foo.com/peter => / usr / www / users / foo / peter
  • www.foo.com/paul => /usr/www/users/foo/paul www.foo.com/paul => / usr / www / users / foo / paul

How can I set $_SERVER['DOCUMENT_ROOT'] to map to /usr/www/users/foo/peter and /usr/www/users/foo/paul respectively? 如何设置$_SERVER['DOCUMENT_ROOT']分别映射到/ usr / www / users / foo / peter和/ usr / www / users / foo / paul?

Thanks. 谢谢。

In my experience I've learned never to trust any $_SERVER variables. 以我的经验,我学会了永远不要相信任何$ _SERVER变量。 Instead, I've set the DOC_ROOT myself using a combination of dirname() and a common include file whose location I am certain will never change. 取而代之的是,我自己使用dirname()和一个公共包含文件的组合来自己设置DOC_ROOT,我确定其位置永远不会改变。 A lot of the common frameworks will do something similar in the dispatch or index file which handles ALL requests. 许多通用框架会在处理所有请求的分派或索引文件中执行类似的操作。

Example Application Folder Structure: 示例应用程序文件夹结构:

-/usr/www/users/
  -peter/
    -script.php
  -paul/
    -script.php
  -index.php
//index.php

DEFINE('ROOT_PATH', dirname(__FILE__));
echo ROOT_PATH; // /usr/www/users/

More info on php's magic constants like __FILE__ 有关php的魔术常量(如__FILE__更多信息

Hope that helps. 希望能有所帮助。

DocumentRoot is exactly that: the file system location of the root ( / ) uri for a specific subdomain. DocumentRoot就是这样的:特定子域的根(/)uri的文件系统位置。

By the time .htaccess is processed, it's already too late to change it. 在处理.htaccess时,更改它已经为时已晚。 Why? 为什么? Well... because the .htaccess file you're accessing is in said file system location! 好吧...因为您正在访问的.htaccess文件在上述文件系统位置中!

Having said that, you can create a new constant and use $_SERVER['DOCUMENT_ROOT'] as part of it. 话虽如此,您可以创建一个新常量并将$_SERVER['DOCUMENT_ROOT']用作其中的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM