Saturday, March 17, 2012

Redirecting based on the URL string using PHP

In many cases, one ends up hosting multiple sites on a single server, shared or VPS.

Also in many other cases a company has multiple domains and multiple page addresses to service clients and prospects from different countries, demographics and walks of life.

And the need of the hour is that one wants to send a user to a different folder on your web site, based on what domain they use? Here we will explain a simple way to do this using PHP.

For instance on my VPS, I have 2 domains pointing: one domain called sampleno1.com and the other one called sampleno2.com. Both point to the IP of the VPS.
We have an index.php file in the root of the web server on the VPS containing the following code.

<?php

if (($_SERVER['HTTP_HOST'] == “sampleno1.com”) || ($_SERVER['HTTP_HOST'] == “www.sampleno1.com”))
{
    header("location: folderforsampleno1");
}

if (($_SERVER['HTTP_HOST'] == “sampleno2.com”) || ($_SERVER['HTTP_HOST'] == “www.sampleno2.com”))
{
    header("location: folderforsampleno2");
}

?>


The code asks the PHP server to see what the HTTP_HOST header is. If it is sampleno1.com or www.sampleno1.com, then it redirects to the folder on the server which contains the code for the site sampleno1.

If it is sampleno2.com or www.sampleno2.com, then it redirects to the folder on the server which contains the code for the site sampleno2

Saturday, March 3, 2012

Find a File containing a specific text string in Ubuntu

Find a File containing a specific text string in Ubuntu can be done using the grep command.

The format is

grep “text string to search” directory-path

Some Examples

Simple String Search in a directory:
Search for a string called "star wars" in all text files located in /home/movies/*.txt directory, use

$ grep "star wars" /home/movies/*.txt

Search for a string in all files in all contained folders

For this, you can search for a text string all files under each directory, recursively with -r option:

$ grep -r "star wars" /home/movies/