2

I have a domain ie test.example.com.

I set the hostname of my server to this by uisng:

hostname test.example.com

I then add a Virtual host in /etc/httpd/conf/httpd.conf

NameVirtualHost *:80

<VirtualHost *:80>
    DocumentRoot /var/www/html/
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot /var/www/html/mydir
    ServerName test.example.com
    ServerAlias www.test.example.com
</VirtualHost>

I then restart the Httpd service. If I try to access test.example.com I get index.php in /var/www/html and not the specified folder. If I change the hostname to something other than this and then restart httpd, the virtual host works as expected, so I can only assume that when there is an incoming connection to the hostname, apache strips the url.

In php when printing

$_SERVER['REQUEST_URI']
$_SERVER['REDIRECT_URL']

I get /. Can anyone tell me how to can overcome the issue or should I keep the hostname set as something else? I have a backup script which uses the hostname as a directory name so I can organise the backups nicely, this is the main reason I have changed the hostname in the first place.

The Humble Rat
  • 233
  • 1
  • 5
  • 20
  • Which address youwould use to access to /var/www/html/? is this folder get any use? if you remove your first virtualhost it will work. – Froggiz Dec 18 '15 at 12:15
  • @Froggiz I generally keep this so that should I wish to access the server using its ip, I can then traverse the directories. – The Humble Rat Dec 18 '15 at 14:00

2 Answers2

2

What i would recommend is changing :

<VirtualHost *:80>
    DocumentRoot /var/www/html/
</VirtualHost>

to :

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

I have seen this several times on customer servers and this has fixed the issue.

I normally point the Virtual Host entry with out a server name to a directory with a coming soon or future home of php file until im ready to take a server or site live as it will take precedence of any future entries.

user9517
  • 115,471
  • 20
  • 215
  • 297
2

In your case, I would replace

NameVirtualHost *:80

<VirtualHost *:80>
    DocumentRoot /var/www/html/
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot /var/www/html/mydir
    ServerName test.example.com
    ServerAlias www.test.example.com
</VirtualHost>

by

<VirtualHost *:80>
    DocumentRoot /var/www/html/mydir
    ServerName test.example.com
    ServerAlias www.test.example.com
</VirtualHost>

# /!\ THIS HAS TO BE ON THE LAST POSITION /!\
<VirtualHost *:80 *:443>
ServerName localhost
ServerAlias *
DocumentRoot /var/www/html/
</VirtualHost>

Like that all request other than test.example.com & www.test.example.com will deliver /var/www/html/

More information : NameVirtualHost has no use in your case, and has been removed in Apache 2.4 https://httpd.apache.org/docs/2.4/en/upgrading.html

Froggiz
  • 3,043
  • 1
  • 19
  • 30