Page 1 of 1

PHP redirect

Posted: Wed Oct 23, 2002 4:27 pm
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

Posted: Wed Oct 23, 2002 4:55 pm
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/"); 

?>

Posted: Wed Oct 23, 2002 5:00 pm
by rev
Concerning using header(), be sure you do not output anything to the browser before using it else the redirect will fail.

Posted: Wed Oct 23, 2002 5:11 pm
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

Posted: Wed Oct 23, 2002 6:13 pm
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.

Posted: Wed Oct 23, 2002 6:23 pm
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.

Posted: Wed Oct 23, 2002 8:36 pm
by borris
What am I doing wrong... parse error on line 2...

Posted: Wed Oct 23, 2002 9:24 pm
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... :)

Posted: Wed Oct 23, 2002 11:13 pm
by mydimension
oops :oops:, silly me forgot a ). sorry 'bout that, wrote it kinda fast and didn't check it very well.

Posted: Thu Oct 24, 2002 10:13 am
by borris
Awesome!!!!!
Sometimes it just the lil things that matter most!

Thanks guys!

Posted: Thu Oct 24, 2002 12:44 pm
by rev
borris wrote: Sometimes it just the lil things that matter most!
As long as she doesn't say that...