View Full Version : Redirect Script for only certain URLs
Travis250923
04-08-2009, 10:45 AM
I am trying to save some $$ on hosting (I know should probably pony up for separate hosting) and want to redirect two specific URLS that are going to my current site (I have three domains pointing to my site). I have setup the domain pointers through my host and essentially have two sites in one directory. I tried using the below script but it redirects all URLS not just the two I specificed. Any help would be great:
$referer = $_SERVER['HTTP_REFERER'];
if ( $referer != "http://mydomain2.com" || "http://mydomain3.com" ) {
header('Location: http://mydomain2.com/index2.php');
exit;
};
Thanks
Justin Nemeth
04-08-2009, 03:43 PM
I see a few things with the above code that are likely causing some issues. First, after the || you need to add $referer =, the string is probably just equating to true and thus always making the if execute. The other thing to consider is what if they add www or some other subdomain to the beginning of the url? That will not satisfy this explicit string match. I would use strpos() to check for the substring of "mydomain2.com" for example.
A better way to handle this is really outside of PHP altogether and instead use the apache settings. Not sure what sort of settings your host provides, but you can setup virtual hosts in apache and run multiple domains that way. Another way is to use mod_rewrite in a .htaccess file and redirect based on the url in there.
Travis250923
04-08-2009, 08:16 PM
Ok so I tried changing to this:
$referer = $_SERVER['HTTP_REFERER'];
if ($referer != '/mydomain2/' || $referer != '/mydomain3/' ) {
header('Location: http://mydomain2.com/index2.php');
};
and also this:
$referer = $_SERVER['HTTP_REFERER'];
if ($referer != '/mydomain2/' || $referer != '/mydomain3/' ) {
header('Location: http://mydomain2.com/index2.php');
} else{
header('Location: http://www.mydomain.com/index.php');
};
Neither change the behavior.
I then tried:
$referer = $_SERVER['HTTP_REFERER'];
if (preg_match($referer != '/mydomain2/' || $referer != '/mydomain3/' )) {
header('Location: http://mydomain2.com/index2.php');
} else{
header('Location: http://www.mydomain.com/index.php');
};
but this gives a browser error that it is redirecting in a way that will never complete.
Travis250923
04-10-2009, 08:05 PM
I tried this and no change. I can see to get it to redirect all URLs to only one page:
switch ($HTTP_HOST) {
case "/domain2/":
header("Location: /domain2.com/index.php");
break;
case "/domain3/":
header("Location: /domain3/index.php");
break;
default:
header("Location: /index2.php");
};
Ray Borduin
04-13-2009, 07:42 AM
if you add:
die($HTTP_HOST);
does it print the values you expect for the domains?
Travis250923
04-18-2009, 09:18 AM
So I am not getting anything w/that code snippet. Perhaps I'm not placing it right (I don't know much PHP). On a side I get the page to redirect with the last script but it always goes to the default location but does change the URL.
Thanks
Justin Nemeth
04-20-2009, 04:11 PM
Try using $_SERVER['HTTP_HOST'] instead of $HTTP_HOST. You can then try die($_SERVER['HTTP_HOST'] to see what the value is set to.
Travis250923
04-21-2009, 05:23 PM
So it prints the URL that I enter the site from, so if I use http://www.domain.com it prints http://www.domain.com and if I use http://www.domain2.com it prints http://www.domain2.com.
The problem is it is not redirecting to the page I want. If they use http://www.domain.com I want it to go to http://domain.com/domain.php and if they use http://www.domain2.com I want it to to http://www.domain2.com/domain2.php
Ray Borduin
04-22-2009, 09:46 AM
switch ($_SERVER['HTTP_HOST']) {
case "http://www.domain.com":
header("Location: /domain.php");
break;
case "http://www.domain2.com":
header("Location: /domain2.php");
break;
default:
header("Location: /index2.php");
};
Travis250923
04-23-2009, 05:00 PM
So that does the same thing. No matter what it redirects to the default page.
Travis250923
05-11-2009, 08:17 PM
I gave up. Got separate hosting. Thanks anyway for the help.
vBulletin® v3.8.1, Copyright ©2000-2012, Jelsoft Enterprises Ltd.