Click to See Complete Forum and Search --> : URL REDIRECTION - help


lukey155
02-25-2008, 04:43 PM
hey

was wondering if anyone knows how i can sort out this problem.
I want to know how i can redirect people if they visit say:
mydomain.com they get redirected to
www.mydomain.com ?

I want to know how i could achieve this because i have a small problem with login cookies at mydomain.com where a user isn't logged in, but they are at www.mydomain.com

If someone could give a suggestion it would be most appreciated.

thanks

afigueroa
02-25-2008, 06:01 PM
you could use js:

<script type="text/javascript">self.location.href="bla.com"</script>


or php

<?php
header("Location: bla.com");
?>

drhowarddrfine
02-26-2008, 10:27 AM
This is a simple setting in your server. I assume you run Apache, but I don't, so someone else will have to tell you how to set that.

kiwibrit
02-26-2008, 11:19 AM
I think I'd do it with htaccess and a 301 re-direct.

kelly23
02-26-2008, 01:06 PM
Try this in your .htaccess file:


Options +FollowSymLinks
RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\.yourdomain\.com
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]


... assuming apache server.

SyCo
02-26-2008, 01:30 PM
Don't use javascript to redirect or google will stop liking you and this code isn't going to work either.(sorry afigueroa)<?php
header("Location: bla.com");
?>because you'll get redirected to bla.com regardless of the domain.

You'll first have to detect the domain name and then redirect if necessary. eg.

if($_SERVER['SERVER_NAME']=="www.mydomain.com"){
header("Location: http://mydomain.com");
}

This will work on IIS and Apache and doesn't require new modules. This must go before any page output (including spaces) and before you set or read the cookie.

I always add a header.php include to the top of every script I work on to give me a globally updateable include for just this kind of thing. A footer.php include too when I remember. :) Even if there's nothing to go in it at the time, you never know!