Redirecting in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dirgeshp
Forum Newbie
Posts: 23
Joined: Thu Jun 22, 2006 11:40 am

Redirecting in PHP

Post by dirgeshp »

My Question is

I have a domain name and what I wanna be able to do is. ... have users go to X.Domain.com and then have that redirect to domain.com/users/X

Can someone tell me how to do this?

x.domain.com -> domain.com/users/x
y.domain.com -> domain.com/users/y

Please reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Code: Select all

header('location: http://www.domain2.com/somedir');
Edit: You can also use a META redirect or javascript...

But i'd go with PHP example given above...it just makes life easier :)
Last edited by alex.barylski on Thu Jun 22, 2006 11:45 am, edited 1 time in total.
dirgeshp
Forum Newbie
Posts: 23
Joined: Thu Jun 22, 2006 11:40 am

Post by dirgeshp »

Yea, but where do i put that...

you have to see that users are not going to domain.com anymore they are going to x.domain.com

so where do i put that HEADER info?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

I don't know if this will work or not, it's just some code I threw together from the php manual...

Code: Select all

$CurrentAddress = strtolower(strtok($_SERVER['SERVER_PROTOCOL'], '/')).'://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];

switch($CurrentAddress) {
  case 'http://x.thisaddress.com':
    header("Location: http://www.domain1.com");
    exit();
    break;
  case 'http://y.thataddress.com':
    header("Location: http://www.domain2.com");
    exit();
    break;
  default:
    header("Location: http://www.domain1.com");
    exit();
}
dirgeshp
Forum Newbie
Posts: 23
Joined: Thu Jun 22, 2006 11:40 am

Post by dirgeshp »

where do i put that code?

index.php ?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

At the top of the page that you want to redirect, make sure there is no space before the <?php, it needs to be at the very top. If anything gets sent before the header redirect it will throw an error.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Can you use mod_rewrite?
dirgeshp
Forum Newbie
Posts: 23
Joined: Thu Jun 22, 2006 11:40 am

Post by dirgeshp »

how can i check to see if i can?

if so, what would i have to do...

i tried placing a script in the .htaccess file...but it screwed up everything

so i guess i might not be able to
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

The code I posted is PHP...

It should go in the page you want to do the redirection, such as index.php, between <?php and ?> tags.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

dirgeshp wrote:how can i check to see if i can?

if so, what would i have to do...

i tried placing a script in the .htaccess file...but it screwed up everything

so i guess i might not be able to
What was in your .htaccess file? Just putting a .htaccess file up shouldn't screw anything up unless what was in the .htaccess was not correct.
Post Reply