Asked  9 Months ago    Answers:  5   Viewed   56 times

I have an Apache virtual host that looks like this:

<VirtualHost *:80>
    ServerName host1.example.com
    ServerAlias host2.example.com
    DocumentRoot /srv/web/host1.example.com/pub
</VirtualHost>

I have the following file:

/srv/web/host1.example.com/pub/test.php

I am requesting the following URL (notice that I am using the ServerAlias, not the ServerName:

http://host2.example.com/test.php

I'd like to have test.php output the ServerName variable somehow (host1.example.com). Is there any way to do that?

 Answers

67

Apparently, when you access a VirtualHost through the alias, there is no trace of the original SERVER_NAME in any of the $_SERVER variables.

The only idea that comes to mind is setting an environment variable:

SetEnv MY_HOST host1.example.com

this should set the value of $_SERVER["MY_HOST"] to the correct host name. No guarantees though, I have never tried this in practice.

Wednesday, March 31, 2021
 
hjalpmig
answered 9 Months ago
11

PHP does not support named parameters for functions per se. However, there are some ways to get around this:

  1. Use an array as the only argument for the function. Then you can pull values from the array. This allows for using named arguments in the array.
  2. If you want to allow optional number of arguments depending on context, then you can use func_num_args and func_get_args rather than specifying the valid parameters in the function definition. Then based on number of arguments, string lengths, etc you can determine what to do.
  3. Pass a null value to any argument you don't want to specify. Not really getting around it, but it works.
  4. If you're working in an object context, then you can use the magic method __call() to handle these types of requests so that you can route to private methods based on what arguments have been passed.
Wednesday, March 31, 2021
 
Anele
answered 9 Months ago
45
" http://162.243.93.216/info.php then /var/www/html/info.php is opened "

I am assuming this already works (If not, uncomment the ServerAlias line shown in the conf below)

You now want to map

http://162.243.93.216/projecta/ to /var/www/projecta
http://162.243.93.216/projectb/ to /var/www/projectb

For this you need to use the Apache Alias directive.

Update your 000-default.conf file to:

<VirtualHost *:80>
    # ServerAlias 162.243.93.216
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    Alias /projecta /var/www/projecta
    Alias /projectb /var/www/projectb

    <Directory /var/www/>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
     </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Wednesday, March 31, 2021
 
e_i_pi
answered 9 Months ago
49

Presuming you're doing something like:

$result = $statement->execute();

You can get the number of rows with

$result->num_rows;

See the manual.

Saturday, May 29, 2021
 
Oshrib
answered 7 Months ago
12

I'm such a noob, it was a real simple solution.

ServerName does not allow wildcards, but ServerAlias does, so I just duplicated the servername again.

Here is the final minimized config for domains anyone who happens to find this answer (I hate it when you search on google and find questions but no answers):

<VirtualHost [ip here]:80>
    ServerName domain1.com
    ServerAlias www.domain1.com
    DocumentRoot /home/user1/public_html
</VirtualHost>
<VirtualHost [ip here]:80>
    ServerName domain1.com #<-- The fix. Wildcards not supported in ServerName
    ServerAlias *.domain1.com
    VirtualDocumentRoot /home/user1/subdomains/%-3+
</VirtualHost>
Wednesday, August 18, 2021
 
turik
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share