Asked  9 Months ago    Answers:  5   Viewed   56 times

I have an Apache virtual host that looks like this:

<VirtualHost *:80>
    DocumentRoot /srv/web/

I have the following file:


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

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



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:


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
answered 9 Months ago

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
answered 9 Months ago
" 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 to /var/www/projecta to /var/www/projectb

For this you need to use the Apache Alias directive.

Update your 000-default.conf file to:

<VirtualHost *:80>
    # ServerAlias
    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

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

Presuming you're doing something like:

$result = $statement->execute();

You can get the number of rows with


See the manual.

Saturday, May 29, 2021
answered 7 Months ago

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>
    DocumentRoot /home/user1/public_html
<VirtualHost [ip here]:80>
    ServerName #<-- The fix. Wildcards not supported in ServerName
    ServerAlias *
    VirtualDocumentRoot /home/user1/subdomains/%-3+
Wednesday, August 18, 2021
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 :