Web Programming Hack – How to Redirect based on Domain Name Referrer or IP address!

My friend Josh over at Unconventional Marketing just pinged me via GoogleTalk on how to redirect your web site visitors based on IP address.   Well, I figure this might be great information for others as well so I found a solution.

This method will work well because you can redirect single web pages using PHP instead of using .htaccess.  I think php files are a lot more versatile and useful anyways.

Redirect based on domain:

$referrer = $_SERVER[‘HTTP_REFERER’];
if (preg_match(“/site1.com/”,$referrer)) {
header(‘Location: http://www.customercare.com/page-site1.html’);
} elseif (preg_match(“/site2.com/”,$referrer)) {
header(‘Location: http://www.customercare.com/page-site2.html’);
} else {
header(‘Location: http://www.customercare.com/home-page.html’);

Redirect based on IP:

$visitor = $_SERVER[‘REMOTE_ADDR’];
if (preg_match(“/”,$visitor)) {
header(‘Location: http://www.yoursite.com/thank-you.html’);
} else {
header(‘Location: http://www.yoursite.com/home-page.html’);

via phpjabbers

Leave a Reply

Your email address will not be published.

Check out more interesting categories: Blog, DIY, Featured Hacks, Hack, technology, Web, Wordpress.

Related News and Resources