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
bloodl
Forum Commoner
Posts: 48
Joined: Thu Jun 21, 2007 12:33 am

php redirect

Post 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
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: php redirect

Post 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).
manixrock
Forum Commoner
Posts: 45
Joined: Sun Jul 20, 2008 6:38 pm

Re: php redirect

Post 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");
bloodl
Forum Commoner
Posts: 48
Joined: Thu Jun 21, 2007 12:33 am

Re: php redirect

Post 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
manixrock
Forum Commoner
Posts: 45
Joined: Sun Jul 20, 2008 6:38 pm

Re: php redirect

Post 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();
}
bloodl
Forum Commoner
Posts: 48
Joined: Thu Jun 21, 2007 12:33 am

Re: php redirect

Post 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?
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: php redirect

Post by jaoudestudios »

This might be helpful on url redirect masking...
http://www.forum.jaoudestudios.com/view ... ?f=13&t=86
bloodl
Forum Commoner
Posts: 48
Joined: Thu Jun 21, 2007 12:33 am

Re: php redirect

Post by bloodl »

Do you need apache to allow .htaccess files to work?
Post Reply