Solved

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

Posted on 2007-04-05
2
202 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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
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.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now