Topic: Help me redirect http://domain.com to http://www.domain.com

This is to fix the problem where users keep getting logged out.

http://www.domain.com/forum shows users logged in (correct)
http://domain.com/forum shows users logged out even though they are logged in

So I would like to set up my server somehow to permanently redirect http://domain.com to http://www.domain.com and that will fix the problem for me.

Is this possible and can someone help me do it?
Thanks smile
Mark

2 (edited by MattF 2009-01-10 14:11)

Re: Help me redirect http://domain.com to http://www.domain.com

Did I not say your problem was related to the cookie path? There is cartloads of info on here regarding the subject.

3 (edited by User33 2009-01-10 14:25)

Re: Help me redirect http://domain.com to http://www.domain.com

if (strstr( $_SERVER['HTTP_HOST'], 'www.' ) === false)
{
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://www' . $_SERVER['REQUEST_URI']);
exit();
}

At the beginning of your config file, I guess.

Re: Help me redirect http://domain.com to http://www.domain.com

MattF wrote:

Did I not say your problem was related to the cookie path? There is cartloads of info on here regarding the subject.

I have not found a discussion that details how to fix the login problem sad
I don't know how to edit cookies either.

Cheers for that Garciat, do you mean my punbb config.php file? or is there a config file for my server somewhere do you think?

Re: Help me redirect http://domain.com to http://www.domain.com

Yes, your PunBB config file. (It will only work for your forum)

6

Re: Help me redirect http://domain.com to http://www.domain.com

esupergood wrote:

I have not found a discussion that details how to fix the login problem

It's there somewhere. It may be the correct search terms that are hard to find. big_smile

Anyhows, if you do a 301, (only do that if you want www.[domain] permanently redirected to .[domain]), you'd be better off doing it correctly via the httpd server itself. Apache does it via htaccess, I believe. If you want a non permanent redirect, (i.e: the client will recheck the redirect upon every visit), then don't use 301. I believe, (though you had best double check), that a 302 is the code for that scenario.

Re: Help me redirect http://domain.com to http://www.domain.com

Cheers for this smile
I'll try Garciat's suggestion first.

Re: Help me redirect http://domain.com to http://www.domain.com

Garciat I tried that code at the front of my config.php file and got the following error when trying to view the forum

if (strstr( $_SERVER['HTTP_HOST'], 'www.' ) === false) { header('HTTP/1.1 301 Moved Permanently'); header('Location: http://www' . $_SERVER['REQUEST_URI']); exit(); } 
Warning: Cannot modify header information - headers already sent by (output started at /home/mark4321/public_html/forum/config.php:7) in /home/mark4321/public_html/forum/include/functions.php on line 152

Warning: Cannot modify header information - headers already sent by (output started at /home/mark4321/public_html/forum/config.php:7) in /home/mark4321/public_html/forum/include/functions.php on line 155

Warning: Cannot modify header information - headers already sent by (output started at /home/mark4321/public_html/forum/config.php:7) in /home/mark4321/public_html/forum/header.php on line 16

Warning: Cannot modify header information - headers already sent by (output started at /home/mark4321/public_html/forum/config.php:7) in /home/mark4321/public_html/forum/header.php on line 17

Warning: Cannot modify header information - headers already sent by (output started at /home/mark4321/public_html/forum/config.php:7) in /home/mark4321/public_html/forum/header.php on line 18

Warning: Cannot modify header information - headers already sent by (output started at /home/mark4321/public_html/forum/config.php:7) in /home/mark4321/public_html/forum/header.php on line 19

Warning: Cannot modify header information - headers already sent by (output started at /home/mark4321/public_html/forum/config.php:7) in /home/mark4321/public_html/forum/header.php on line 22

Any clues?

Re: Help me redirect http://domain.com to http://www.domain.com

Then try at the front of 'include/common.php'.

Btw, the previous code I gave was crap. Use this:

if (strstr( $_SERVER['HTTP_HOST'], 'www.' ) === false)
{
    header('HTTP/1.1 302 Found');
    header('Location: http://www.' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
    exit;
}

Re: Help me redirect http://domain.com to http://www.domain.com

Cheers smile
But using the new code and adding to the front of the common.php file, this time I get....

if (strstr( $_SERVER['HTTP_HOST'], 'www.' ) === false) { header('HTTP/1.1 302 Found'); header('Location: http://www.' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']); exit; } 
Warning: Cannot modify header information - headers already sent by (output started at /home/mark4321/public_html/forum/include/common.php:7) in /home/mark4321/public_html/forum/include/functions.php on line 152

Warning: Cannot modify header information - headers already sent by (output started at /home/mark4321/public_html/forum/include/common.php:7) in /home/mark4321/public_html/forum/include/functions.php on line 155

Warning: Cannot modify header information - headers already sent by (output started at /home/mark4321/public_html/forum/include/common.php:7) in /home/mark4321/public_html/forum/header.php on line 16

Warning: Cannot modify header information - headers already sent by (output started at /home/mark4321/public_html/forum/include/common.php:7) in /home/mark4321/public_html/forum/header.php on line 17

Warning: Cannot modify header information - headers already sent by (output started at /home/mark4321/public_html/forum/include/common.php:7) in /home/mark4321/public_html/forum/header.php on line 18

Warning: Cannot modify header information - headers already sent by (output started at /home/mark4321/public_html/forum/include/common.php:7) in /home/mark4321/public_html/forum/header.php on line 19

Warning: Cannot modify header information - headers already sent by (output started at /home/mark4321/public_html/forum/include/common.php:7) in /home/mark4321/public_html/forum/header.php on line 22

11

Re: Help me redirect http://domain.com to http://www.domain.com

After the '<?php'

Re: Help me redirect http://domain.com to http://www.domain.com

aha! let me try again smile
It works!!!
Smashing, thanks a lot Garciat, this should help a few people for certain.
wink