Solved

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

Posted on 2007-04-05
2
206 Views
Last Modified: 2008-07-03
I have a page at:

www.myDomain.com/folder1/folder2/filename.php

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

<a href="www.test.com> click here for test.com </a>

it goes to:
www.myDomain.com/folder1/folder2/test.com
instead of
http://www.test.com


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  "www.domain.com" 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?

0
Comment
Question by:rohgan
2 Comments
 
LVL 23

Assisted Solution

by:Stacy Spear
Stacy Spear earned 250 total points
ID: 18859243
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 test.com </a>
0
 
LVL 15

Accepted Solution

by:
Tomeeboy earned 250 total points
ID: 18859264
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):

<?php

$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!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question