Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

php redirect for two URLs

Status
Not open for further replies.

calidude

Technical User
Dec 11, 2003
24
0
0
US
I am trying to set up a redirect based on the URL requested. Below is my code. I get the following error Parse error: syntax error, unexpected '(' in /vservers/mysite/htdocs/home.php on line 4.

Any idea where I am going wrong?


<?php
$domain = $_SERVER["HTTP_HOST"];
if (($domain == “myfirstdomain.com”)
($domain == “ {
header(”location:}
if (($domain == “myseconddomain.com”)
($domain == “ {
header(”location:}
?>
 
Well, you're missing a boolean operator in your if condition. What you've posted isn't valid syntax. You probably wanted a logical or in there.
Code:
if (($domain == "myfirstdomain.com") [COLOR=red]||[/color]
   ($domain == "[URL unfurl="true"]www.myfirstdomain.com"))[/URL] {
 
Hmmmm. I added the || but now I get

Parse error: syntax error, unexpected ':' in /vservers/mysite/htdocs/home.php on line 5

Thanks for the help.
Bob
 
Code:
<?php 
$domain = explode ('.', $_SERVER["HTTP_HOST"]);
$domain = strtolower($domain[count($domain) - 1]);

switch ($domain){
 case 'myfirstdomain':
 $location = '[URL unfurl="true"]http://www.myfirstdomain.com/photocart/index.php';[/URL]
 break;
 case 'myseconddomain':
  $location = '[URL unfurl="true"]http://www.myseconddomain.com';//note[/URL] that there is a never ending loop here
  break;
} //end switch

if (!empty($location)) {
 header ("Location: $location");
 exit();
}
?>
 
I now have

Parse error: syntax error, unexpected ':' in /vservers/mydomain/htdocs/home.php on line 5


<?php
$domain = $_SERVER["HTTP_HOST"];
if (($domain == “mydomain.com”)||
($domain == “ {
header(”location: }
if (($domain == “mydomain2.com”)||
($domain == “ {
header(”location: }

?>
 
you are not copy-pasting faithfully. the error you are getting derives from a mismatch in quotes, most probably in line 3 or 4. your pasted version does not show this error.
 
jpadie, I tried your code but only get a blank page.
 
ok, here is a strange one. I copied and pasted what I had posted here into my page and now it works. I have no idea why. Thanks everyone for the help.

Perplexed Bob
 
for my code this line was incorrect
Code:
$domain = strtolower($domain[count($domain) - [red]2[/red]]);
and actually this is not a great way to do things if you are using a mixture of, say, .co.uk and .com domains. it would be better to use a preg_match like so:

Code:
$domain = preg_replace('/^[URL unfurl="true"]www\./i',[/URL] '', $domain);
$domain = strtolower($domain);

//and then go into the switch on the full domain name minus the [URL unfurl="true"]www bit[/URL]
 
You should modify your header() functions to include a proper response code. Probably a 301.
Code:
header("Location: [URL unfurl="true"]http://www.my-domain.com/",TRUE,301)[/URL]


However, would it not be better to do this using htaccess?

Code:
Options +All
RewriteEngine On

RewriteCond %{HTTP_HOST} ^my-domain.co.uk
RewriteRule ^(.*)$ [URL unfurl="true"]http://www\.my-domain\.co\.uk/$1[/URL] [R=permanent,L]

RewriteCond %{HTTP_HOST} ^my-other-domain.co.uk
RewriteRule ^(.*)$ [URL unfurl="true"]http://www\.my-other-domain\.co\.uk/$1[/URL] [R=permanent,L]

--
Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top