Page 1 of 1

php redirect

Posted: Sun Jul 27, 2008 7:34 pm
by bloodl
Hi,

I was wanting to force a page to use www in the url if the www wasn't typed. Does anyone know of a PHP script which does this? And will this make SEO bad?

Cheers,
Doug

Re: php redirect

Posted: Mon Jul 28, 2008 1:47 am
by jaoudestudios
PHP redirect...

Code: Select all

header("LOCATION:index.php");
But yes think it will not be very good for SEO, you should do the redirect in your server setup (config).

Re: php redirect

Posted: Mon Jul 28, 2008 5:52 pm
by manixrock
As jaoudestudios points out, header redirects trough Location header is bad SEO practice. Instead send the 301 status code response:

Code: Select all

header("HTTP/1.0 301 Moved Permanently");

Re: php redirect

Posted: Mon Jul 28, 2008 6:43 pm
by bloodl
Thanks for your responses!

Forgive me for questioning your code, but on first inspection it looks like your code is a redirect without www detection. Would your code only redirect when the www isn't present?

Cheers,
Doug

Re: php redirect

Posted: Mon Jul 28, 2008 6:55 pm
by manixrock
We did not give you the complete solution, but we did give you the biggest piece of the puzzle.

Here, add this to the beginning of whatever page you want you check in:

Code: Select all

if(preg_match('#^https?://www\.#i', $_SERVER['REQUEST_URI']) {
    header("HTTP/1.0 301 Moved Permanently");
    exit();
}

Re: php redirect

Posted: Tue Jul 29, 2008 12:25 am
by bloodl
Hmmm couldn't get it to work. It made my page blank. I put it in a php tag, perhaps I have not implemented it right.

If I goto http://mysite.com, will this code direct me to http://www.mysite.com?

Re: php redirect

Posted: Tue Jul 29, 2008 2:22 am
by jaoudestudios
This might be helpful on url redirect masking...
http://www.forum.jaoudestudios.com/view ... ?f=13&t=86

Re: php redirect

Posted: Wed Jul 30, 2008 6:59 pm
by bloodl
Do you need apache to allow .htaccess files to work?