News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

Add "www." in address bar

Started by Pratt, January 18, 2007, 07:03:12 PM

Previous topic - Next topic

Pratt

Hi there!

Im having a bit of a problem!

I have a website in php i created with help of SSI.php. I made use the ssi_login function, so that everyuser can login from that site.

I want to make a script in php to redirect everyone that goes to http://domain.com to http://www.domain.com (add the www`s before). something like index.php?www in SMF.

Can someone give me a hand here?

My main problem is that, if i login into my forum at http://www.domain.com/forum and then go to the root site http://domain.com (without the www's) i appear as not logged in! and i want to correct that, so i figured out, it is simplier, if i could just redirect the domain.com to www.domain.com



gururug

header ("Location: hxxp:www.site.com [nonactive]")

Aquilo

you can't just use a header blindly to go to www.site.com it will loop for ever, never resolving!

I have a more complex problem, I have two domains that point to the same place "one parked on the other" plus I don't want index.php in the url so I have this in SMF's index.php

// Rebuild proper HTTP_HOST
switch (true) {
case (preg_match('/aquilo.us$/', $_SERVER['HTTP_HOST']) === 1) :
$URL = 'www.northwindpagans.com' .  $_SERVER['REQUEST_URI'];

$URL = str_replace("/index.php", "/", $URL);
$q = ''; //empty($_SERVER['QUERY_STRING']) ? '' : '?'.$_SERVER['QUERY_STRING'];

header('HTTP/1.1 301 Moved Permanently');
header('Location:' . (($_SERVER["HTTPS"] == 'on')?'https://':'http://') . $URL.$q);
exit();
break;
case (preg_match('/^www./', $_SERVER['HTTP_HOST']) === 0) :
$URL = 'www.northwindpagans.com' .  $_SERVER['REQUEST_URI'];

$URL = str_replace("/index.php", "/", $URL);
$q = ''; //empty($_SERVER['QUERY_STRING']) ? '' : '?'.$_SERVER['QUERY_STRING'];

header('HTTP/1.1 301 Moved Permanently');
header('Location:' . (($_SERVER["HTTPS"] == 'on')?'https://':'http://') . $URL.$q);
exit();
break;
case (preg_match('/index.php/', $_SERVER['REQUEST_URI']) === 1) :
$URL = 'www.northwindpagans.com' .  $_SERVER['REQUEST_URI'];

$URL = str_replace("/index.php", "/", $URL);
$q = ''; //empty($_SERVER['QUERY_STRING']) ? '' : '?'.$_SERVER['QUERY_STRING'];

header('HTTP/1.1 301 Moved Permanently');
header('Location:' . (($_SERVER["HTTPS"] == 'on')?'https://':'http://') . $URL.$q);
exit();
break;
}


your answer will be much simpler!
edit and add this just under SMF's commentary in index.php
so that it's before anything else but after comments!

if (preg_match('/^www./', $_SERVER['HTTP_HOST']) === 0)
{
$URL = 'www.yourdomain.com' .  $_SERVER['REQUEST_URI'];

header('HTTP/1.1 301 Moved Permanently');
header('Location:' . (($_SERVER["HTTPS"] == 'on')?'https://':'http://') . $URL);
exit();
}


don't worry this is complete and will let the forum work, the path and query string are both in $_SERVER['REQUEST_URI'] as you see above in my mess I didn't need it once I found out it comes together!


Aquilo

Aquilo

just a note as to why I have header('HTTP/1.1 301 Moved Permanently');

this let's Google and others know your not messing around with doorways and things just in case they have mysite.com in there database and you flop them around to www.mysite.com they will/could keep both and eventually penalize your site for duel content and telling google "301 Moved Permanently" they will eventually remove mysite.com from there database and just use www.mysite.com.
just SE friendly!

Aquilo

Advertisement: