0

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!

Michael
  • 177
  • 1
  • 2
  • 12

1 Answers1

1

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.

Hello Fishy
  • 729
  • 5
  • 16
  • Hi @Hello Fishy, thanks for answering my question. I added servername for each vhosts to distinguish them and it doesn't work. I followed this documentation of NameVirtualHost. https://httpd.apache.org/docs/2.2/vhosts/name-based.html – Michael Apr 05 '17 at 11:20
  • ´www.mywebsite.com/ph´ is not a servername :( `/ph` is the URI and `www.mywebsite.com` is a servername...so both vhosts still listen to the same port and name -> can't work...either one should have another port or another name. Btw, do you really need two vhosts anyway? – Hello Fishy Apr 05 '17 at 12:09
  • Yes, I really need them. If I visit www.mywebsitemcom/ph, it should use the second vhost. If not, then errors will occur because documentroot is not correct. Thanks Hello Fishy for your help. – Michael Apr 05 '17 at 16:35
  • But you won't need an additional vhost just for that. Just add a RewriteRule like the one above (gimme 2mins^^) – Hello Fishy Apr 05 '17 at 16:39
  • Thanks Hello Fishy. +1 – Michael Apr 05 '17 at 16:51
  • I will test it. :) I have a last question, if I access www.mywebsite.com/ph, does it change the documentroot to var/www/html/php? Thanks Hello Fishy. – Michael Apr 05 '17 at 16:56
  • No, DocumentRoot will stay the same, it is initialized on server-startup not on runtime :/ But as soon as you have another domain-name like www.new.mydomain.com you can set this as `ServerName` for the second vhost and configure a second DocumentRoot there. – Hello Fishy Apr 05 '17 at 16:59
  • It sounds that I need to setup another domain name. Each vhost must have different documentroot. Thanks Hello Fishy – Michael Apr 05 '17 at 17:02
  • You don't really have to change the DocumentRoot. I mean, if you want to use `$_SERVER['DOCUMENT_ROOT']` why not just add `/php` as a string to that, by concatenation?! You could even set an environment variable with the value of your DocumentRoot, 'add' `/php` to that and use this env-var in your PHP-code... – Hello Fishy Apr 05 '17 at 17:04
  • I was thinking to that idea but we have no control to the php files. We just only cloning the php files and anytime it may update. There is a chance that the php file that I modified will be override by the updated ones. – Michael Apr 05 '17 at 17:13
  • But shouldn't the variable stay the same? So if you set a env-var on Apache-side (modified to fit your needs) and call that env-var in PHP code without further manipulation, shouldn't that be good enough? – Hello Fishy Apr 05 '17 at 17:21
  • Hi @Hello Fishy, that is a good idea. Will use that idea. Thanks. – Michael Apr 06 '17 at 10:26