Michael
Michael

Reputation: 187

$_SERVER['DOCUMENT_ROOT'] does not return the expected directory location

I accessed the sub-folder of my website and got this error:

PHP Warning:  include_once(): Failed opening '/var/www/html//include/version.inc' for inclusion (include_path='.:/usr/share/pear:/usr/share/php') in /srv/sparrow/php/include/branches.inc on line 3

It seems that $_SERVER['DOCUMENT_ROOT'] return a wrong directory like this:

/var/www/html/

If I access www.my.website/php, this should be the expected directory that the $_SERVER['DOCUMENT_ROOT'] should return:

/var/www/html/php/

These are my virtualhost configurations:

NameVirtualHost *:80

<VirtualHost *:80>
   ServerName   www.mywebsite.com
   DocumentRoot /var/www/html/

   <Directory /var/www/html/>
     Options -Indexes -MultiViews
   </Directory>
</VirtualHost> 

<VirtualHost *:80>
  ServerName   www.mywebsite.com/ph
  # Webroot of PHP site
  DocumentRoot /var/www/html/php/

  <Directory /var/www/html/php/>
    Options -Indexes -MultiViews
  </Directory>
</VirtualHost> 

How can I configure my virtualhost configurations such that if I access www.my.website/php it should return the DocumentRoot with value /var/www/html/php/ ?

Thanks guys!

Upvotes: 0

Views: 1059

Answers (1)

Hello Fishy
Hello Fishy

Reputation: 739

You have two vhosts on port 80 present. The first wins. You should distinguish them by IP, servername or port. If both run on the same port you'll also need NameVirtualHost 80 if you're using Apache 2.2 (not necessary for 2.4).

I am pretty sure, if you swap the vhosts, you will get /var/www/html/php/ for $_SERVER['DOCUMENT_ROOT']

But still: If you have two vhosts, make them distinguishable!


Update

Additional rewrite (requires mod_rewrite to be activated) in upper vhost, so you can drop the second vhost:

RewriteEngine On
RewriteRule ^/ph/(.*)$ http://www.mywebsite.com/ph/$1 [R=301,L]

or [P,L] if you want to hide that you are rewriting, but [P] requires mod_proxy to be activated as well.

Upvotes: 1

Related Questions