简体   繁体   中英

PHP $_SERVER['DOCUMENT_ROOT'] and htaccess?

I know that you cannot simply set DocumentRoot in htaccess but it's all I have access to. 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.

I then have

  • www.foo.com/peter => /usr/www/users/foo/peter
  • 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?

Thanks.

In my experience I've learned never to trust any $_SERVER variables. 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. A lot of the common frameworks will do something similar in the dispatch or index file which handles ALL requests.

Example Application Folder Structure:

-
  -
    -script.php
  -
    -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__

Hope that helps.

DocumentRoot is exactly that: the file system location of the root ( / ) uri for a specific subdomain.

By the time .htaccess is processed, it's already too late to change it. Why? Well... because the .htaccess file you're accessing is in said file system location!

Having said that, you can create a new constant and use $_SERVER['DOCUMENT_ROOT'] as part of it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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