Problem with relative URLs and prefixing http:// to domain name

I have a page at:

when I edit filename.php to add a link like :

<a href="> click here for </a>

it goes to:
instead of

I am picking up the link information from a datbase so links sometimes have http:// prefix (in which case it works fine) and sometimes just have  "" style links with no http, which become broekn relative URLs.

Is this expected behavior?

 If so, how to check if there is no http prefix to a link string and if so, add the same?

Who is Participating?
TomeeboyConnect With a Mentor Commented:
You will need to have the HTTP:// in your link, or else it will use relative linking and give you the above result.

You could always do some string altering to add the prefix if it doesn't exist, but the www. exists.  For example (in PHP5):


$pos1 = strpos($url, "HTTP://");
if ($pos1 == FALSE) {
     if (strpos($url, "www.") == 1) {
          $url = "http://" . $url;


This way, if there is a www. at the beginning of the URL, but not HTTP prefix, it will add one.  However, if you are actually trying to display a link that would use relative linking, it won't mistakingly add the HTTP prefix, because "www." would not exist at the beginning of the URL.

Hope that helps!
Stacy SpearConnect With a Mentor President/Principal ConsultantCommented:
Use a function that checks the link for the http:// portion. For instance

function chkHTTP($link) {
      if (substr($link,0,7)!="http://") {
            $org = $link;
            $link = "http://".$org;
      return $link;

Then use it like

<a href="<?php echo chkHTTP($link); ?>"> click here for </a>
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.