Solved

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

Posted on 2007-04-05
2
207 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

740 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