Forum Moderators: coopster

Message Too Old, No Replies

Add www to url

         

olokiop

9:56 pm on Feb 1, 2005 (gmt 0)

10+ Year Member



I ran over a script at these forums a while ago that allowed someone to put the www. in front of the url if a visitor did not type it in. I have spent the last 3 hours searching for that script again with no luck. Anyone know how to do this or know where it is posted in these forums?

FYI it was a refresh based on a url if statment.

Alexander2005

10:07 pm on Feb 1, 2005 (gmt 0)

10+ Year Member



Have you considered doing it in javascript, visual quickstart to Javascript has exactly that as an example.
alternatively, have a .inc file in the same directory on the server as your index.html page on the web

olokiop

10:18 pm on Feb 1, 2005 (gmt 0)

10+ Year Member



not yet, but will post this there as well.

olokiop

10:24 pm on Feb 1, 2005 (gmt 0)

10+ Year Member



post the example plz =p

encyclo

10:24 pm on Feb 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you mean something like this:

<?php if ($_SERVER['HTTP_HOST'] != "www.example.com") {
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.example.com".$_SERVER['REQUEST_URI']);
exit;
}
else {
// normal page headers
};
?>

The above sends a 301 Permanent redirect if the URL isn't the www. version (or is anything else), but retaining the full URL details from after the domain name.

olokiop

10:28 pm on Feb 1, 2005 (gmt 0)

10+ Year Member



Not what I ment.

Say a user types 'cnn.com' into their browser. I need the browser or the server to know this and send them to 'www.cnn.com'.

301's will not work for this.

Zipper

12:40 am on Feb 2, 2005 (gmt 0)

10+ Year Member



anything like this? not tested though..


if (substr($_SERVER['HTTP_HOST'], 4)!= "www."){
$url = "http://www." . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
header("Location: $url");
}

encyclo

12:54 am on Feb 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Say a user types 'cnn.com' into their browser. I need the browser or the server to know this and send them to 'www.cnn.com'.

That is exactly what my quoted PHP code does.

olokiop

1:29 am on Feb 2, 2005 (gmt 0)

10+ Year Member



Ah i see encyclo, sorry for the confusion.

I will run tests as soon as I get home.