0

Do you know how to fix next issue? For that go please to http://11klassniki.ru/school_post.php?id_school=2639 and click on the link Официальный сайт: tsarevo-zaimishe-sosh.edusite.ru and you will be transfer to http://11klassniki.ru/tsarevo-zaimishe-sosh.edusite.ru. I don't know why but link begins from main site.

If I would change link in database and add "http://" I didn't cause any problem.

Of course, I can add "http://" to next code:

if (!empty($myrow_vuz ['site'])) {
printf ("<p class='town'>Official site: <span class='town_name'>
<noindex><a href='%s' target = '_blank' rel='nofollow'>%s</a></noindex></span></p>"
, $myrow_vuz["site"], $myrow_vuz["site"]);
}

But I have many links in database which begins from http:// already.

4 Answers4

0

If an href doesn't include a protocol, such as http://, it's assumed to be a relative path. The idea is that you can have a link to /logout without having to reference the hostname. When you link to tsarevo-zaimishe-sosh.edusite.ru without a protocol, it's still interpreted as a relative link.

So, given that you must include http:// in your links, you can either go through your database to tidy up the ones that have it, or write your display code so that it checks whether the http:// is already there and adds it if it is not.

Erin Call
  • 1,764
  • 11
  • 15
0

i believe that you answered your own question. If you can add http:// and it solves the problem then why not do that?

0

You are right in your assesment that you should add http:// to the beginning of your link, or it will be treated as relative instead of absolute.

Since you are using PHP, you can easily check whether your link does in fact start with http://, and if not, prepend it to the link!

Check How to check if a string starts with a specified string? for how to check whether your string starts with http:// and prepend it with the . (dot) operator. Good luck!

Community
  • 1
  • 1
Sanchises
  • 847
  • 4
  • 22
0

You can run a preg_match to check for instances of http:// or https://, and add them when they're not there. However cleaning up your solution to always contain http:// at the beginning of all saved links would still be recommended.

$site = "http://mywebsite.com";
// For replacement of your code, you may remove the line above and uncomment the one below.
//$site = $myrow_vuz ['site'];

if (! empty ( $site )) {
    $site = (preg_match ( "/(^http:\/\/)|(^https:\/\/)/", $site )) ? $site : "http://" . $site;
    printf ( "<p class='town'>Official site: <span class='town_name'><noindex><a href='%s' target = '_blank' rel='nofollow'>%s</a></noindex></span></p>", $site, $site );
}
Baldvin Th
  • 280
  • 2
  • 11
  • You should probably tag this question as answered then, so that time from others won't go into fixing this problem. – Baldvin Th Sep 20 '14 at 20:08