Default Virtual Host in Apache

If you have multiple domains installed in a virtual host configuration with one IP address in Apache, the IP address may not resolve to the domain you prefer. Let me explain.

For example, you have the following domains running on an Ubuntu Server with one IP address.

abc.com
cde.com
klm.com
xyz.com

All the domains are resolving as expected on the browser.

However, if you type the IP address on the browser, it only defaults to the first domain found in the /etc/apache2/sites-available directory, which is most likely abc.com.

If you want the IP address to default to another domain, such as klm.com for example, you will need to edit the /etc/apache2/httpd.conf file and add the following entries.

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

Those are the only entries you’ll need. Typing the IP address on the browser will now default to the contents of klm.com. All the other domains are still accessible via domain names on the browser.

Validate IP Addresses in PHP

Let’s say you were given a task to validate an IP address in a custom script. One approach is to use regular expressions and use pattern matching to see if user input is correct or not. In this example, we will use a regular expression below to validate IP addresses.

We assume user input is done via a form. First things first, we need to sanitize the input. Once sanitized, we can then test the input against the regular expression using a PHP function called the ereg().

// the regular expression for valid ip addresses
$reg_ex = '/^((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))*$/';
// sanitize input from form
$ip_address = addslashes(htmlspecialchars(strip_tags(trim($_POST['ip_address']))));
// test input against the regular expression
if (ereg($reg_ex, $ip_address)) { 
   // ip address is valid
} else {
   // ip address is not valid
}

The result is, we now have a facility to check the validity of IP addresses. We can now perform additional steps when the IP address is valid, or display errors if the IP address is invalid.

Display Your WAN IP Address

Have you ever wondered how dozens of websites can display your WAN IP address? The script is quite simple. You can probably host it if you have access to a web server. All you need to do is create a PHP file. Place the code below. Save. Upload the file to a web server and run the script. That’s it.

<?php
$ip = getenv(REMOTE_ADDR);
echo “Your IP address is:. $ip;
?>

Running Out of IPv4 Addresses

Apparently, IPv4 is running out of IP addresses according to NRO (Number Resource Administration). The number of available IP addresses is less than 10 percent and will run out by 2012. What this means is we will see IPv6 being implemented sometime next year. It’s up to ISP’s and router manufacturers to include IPv6 functionality to make the change. Local network administrators can switch their local networks now, independent of whether their ISP make a switch sooner or later. Most operating systems including Windows XP, Windows Vista, Windows 7, Mac OS and Linux already support IPv6.