代理网站中的相对网址不起作用 [英] Relative urls in a proxied website don't work

查看:42
本文介绍了代理网站中的相对网址不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 PHP 中,我编写了一个接受 url、用户代理和其他设置的代理函数.然后该函数向网站发出 curl 请求,并将带有正确 html 内容类型标头的输出打印到 iframe 中(这是必要的,只是因为我需要更改一些标头).

该代理输出通常包含大量具有相对 URL 的资产,并且实际上继承了我站点的主机名,而不是代理站点:

示例:[http://MYSITE.com/proxy?url=http://somesite.com] 将返回 [http://somesite.com]<的 html/p>

在响应html中,有这样的东西:

<link rel="apple-touch-icon-precomposed" sizes="144x144" href="assets/ico/apple-touch-icon-144-precomposed.png">

问题:

它不是在 http://somesite.com/assets/ico/apple-touch-icon-144-precomposed.png 上寻找该资产,而是尝试在http://MYSITE.com/assets/ico/apple-touch-icon-144-precomposed.png 这是错误的.

问题:

我需要做什么才能让他们的相对路径资产通过代理正确加载?

解决方案

标签?你可以把它放在头部,它会通知浏览器使用什么作为页面上所有相对 URL 的基本路径:

<base href="http://somesite.com/">

您可以将它添加到您使用 DOMDocument(注意这是针对 PHP5.4 的,因为数组取消引用,但对于早期版本很容易修复):

if($contentType == 'text/html') {$doc = DOMDocument::loadHTML($html);$head = $doc->getElementsByTagName('head')[0];if(count($head->getElementsByTagName('base')) == 0) {$base = DOMDocument::createElement('base');$base->setAttribute('href', $urlOfPageDir);}$head->appendChild($base);echo $doc->saveHTML();}

注意 $urlOfPageDir 必须是页面所在目录的绝对 URL.有关基本标签的更多信息,请参阅此 SO 问题:是否推荐使用 <base>html标签?

in PHP, I've written a proxy function that accepts a url, user agent, and other settings. Then the function makes a curl request for the website, and prints out that output with proper html content type headers into an iframe (this is necessary only because of my need to change some headers).

That proxied output often has lots of assets with relative URLS and actually inheret the hostname of my site, not the proxied site:

example: [http://MYSITE.com/proxy?url=http://somesite.com] would return the html of [http://somesite.com]

in the response html, there is stuff like this:

<link rel="apple-touch-icon-precomposed" sizes="144x144" href="assets/ico/apple-touch-icon-144-precomposed.png">

The problem:

Instead of the asset looking for that asset at http://somesite.com/assets/ico/apple-touch-icon-144-precomposed.png, it actually tries to find it at http://MYSITE.com/assets/ico/apple-touch-icon-144-precomposed.png which is wrong.

The Question:

What do i need to do to get their relative-path assets to load properly via the proxy?

解决方案

How about the <base> tag? You can place it in the head and it will inform the browser what to use as the base path for all relative URLs on the page:

<head>
    <base href="http://somesite.com/">
</head>

You could add it to each page that you serve with DOMDocument (Note this is for PHP5.4 because of the array dereferencing, but that's easy fixed for earlier versions):

if($contentType == 'text/html') {
    $doc = DOMDocument::loadHTML($html);
    $head = $doc->getElementsByTagName('head')[0];

    if(count($head->getElementsByTagName('base')) == 0) {
        $base = DOMDocument::createElement('base');
        $base->setAttribute('href', $urlOfPageDir);
    }

    $head->appendChild($base);
    echo $doc->saveHTML();
}

Take note that $urlOfPageDir must be the absolute URL of the directory in which the page resides. See this SO question for more on the base tag: Is it recommended to use the <base> html tag?

这篇关于代理网站中的相对网址不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆