简体   繁体   English

从主站点开始链接到另一个站点

[英]link to another site begins from main site

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 . 为此,请访问http://11klassniki.ru/school_post.php?id_school=2639 ,然后单击链接Официальныйсайт:tsarevo-zaimishe-sosh.edusite.ru,您将被转移到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. 如果我要更改数据库中的链接并添加“ http://”,则不会造成任何问题。

Of course, I can add "http://" to next code: 当然,我可以在下一个代码中添加“ http://”:

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. 但是我已经从http://开头的数据库中有很多链接。

If an href doesn't include a protocol, such as http:// , it's assumed to be a relative path. 如果href不包含协议,例如http:// ,则假定它是相对路径。 The idea is that you can have a link to /logout without having to reference the hostname. 这个想法是您可以链接到/logout而不必引用主机名。 When you link to tsarevo-zaimishe-sosh.edusite.ru without a protocol, it's still interpreted as a relative link. 当您不使用协议链接到tsarevo-zaimishe-sosh.edusite.ru ,它仍然被解释为相对链接。

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. 因此,假设您必须在链接中包含http:// ,则可以遍历数据库以整理包含该链接的数据库,也可以编写显示代码以检查http://是否已存在,以及如果不是,则添加它。

i believe that you answered your own question. 我相信你回答了自己的问题。 If you can add http:// and it solves the problem then why not do that? 如果您可以添加http://并解决了问题,那为什么不这样做呢?

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. 您的评估是正确的,您应该在链接的开头添加http:// ,否则它将被视为相对的,而不是绝对的。

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! 由于您使用的是PHP,因此您可以轻松地检查您的链接是否实际上以http://开头,如果不是,则将其放在该链接之前!

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 . 有关如何检查您的字符串是否以http://开头并以开头的说明. (dot) operator. (点)运算符。 Good luck! 祝好运!

You can run a preg_match to check for instances of http:// or https://, and add them when they're not there. 您可以运行preg_match来检查http://或https://的实例,并在不存在时添加它们。 However cleaning up your solution to always contain http:// at the beginning of all saved links would still be recommended. 但是,仍然建议您将解决方案清理为始终在所有已保存链接的开头始终包含http://。

$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 );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM