Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-04-05
2
Medium Priority
?
211 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 1000 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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …

715 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