PHP redirect

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
borris
Forum Newbie
Posts: 4
Joined: Wed Oct 23, 2002 4:27 pm

PHP redirect

Post by borris »

OK...

I have 2 domains, one aliased to the other, both pull up the same content, and the urls stay the same, I want to have one url redirect to the other...

ie:

http://www.domain.com
http://www.domain2.com

So, if http://www.domain2.com is entered, it redirects to http://www.domain.com

Any help much appreciated!

Revised: same index file and I do not have access to the .htaccess file
Last edited by borris on Wed Oct 23, 2002 6:05 pm, edited 1 time in total.
sinewave
Forum Commoner
Posts: 41
Joined: Tue Sep 10, 2002 4:35 pm
Location: Canada

Post by sinewave »

on the index.html of http://www.domain2.com (or whatever is the default file) put:

Code: Select all

<?php
header("Location: http://www.domain.com/"); 

?>
rev
Forum Commoner
Posts: 52
Joined: Wed Oct 02, 2002 3:58 pm
Location: Atlanta, GA

Post by rev »

Concerning using header(), be sure you do not output anything to the browser before using it else the redirect will fail.
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

using an index.php file won't work because both domains access the same index.php file. doing that would result in a nasty loop.

if you can use .htaccess you might try this:

Code: Select all

RewriteCond %{HTTP_HOST} ^*domain2.com
RewriteRule ^(.*) /$1 їR]
i haven't tested this but according to the apahce manual it should work
borris
Forum Newbie
Posts: 4
Joined: Wed Oct 23, 2002 4:27 pm

Post by borris »

Thanks for the ideas,
Both domains use the same index file and I cannot modify the .htaccess file. Just in case it matters PHP is running in safe mode.
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

put this at the top of your index.php to check which domain is being used and then redirect to the preferred one if the secondary one was used.

Code: Select all

<?php
if (preg_match("/domain2/", $_SERVERї'HTTP_HOST']) {
    header("Location: http://www.domain.com");
}
?>
the regular expression could use some work but it should still work.
borris
Forum Newbie
Posts: 4
Joined: Wed Oct 23, 2002 4:27 pm

Post by borris »

What am I doing wrong... parse error on line 2...
sav
Forum Newbie
Posts: 8
Joined: Mon Oct 21, 2002 7:29 pm

Post by sav »

change that code to:

Code: Select all

<?php
if (preg_match("/domain2/", $_SERVERї'HTTP_HOST'])) {
    header("Location: http://www.domain.com");
}
?>
It works now... :)
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

oops :oops:, silly me forgot a ). sorry 'bout that, wrote it kinda fast and didn't check it very well.
borris
Forum Newbie
Posts: 4
Joined: Wed Oct 23, 2002 4:27 pm

Post by borris »

Awesome!!!!!
Sometimes it just the lil things that matter most!

Thanks guys!
rev
Forum Commoner
Posts: 52
Joined: Wed Oct 02, 2002 3:58 pm
Location: Atlanta, GA

Post by rev »

borris wrote: Sometimes it just the lil things that matter most!
As long as she doesn't say that...
Post Reply