How to change the host address where requests "src" and "href" go?

Asked

Viewed 70 times

0

Example: I give one file_get_contents('http://youtube.com') and then, should we src and href html does not have the whole path, but only src="/pasta/arquivo.ext", instead of src="https://youtube.com/pasta/arquivo.ext", the requests will give all 404 not found, because it fetches on my server (localhost).

Is there any parameter of header http that I can switch to indicate where I want these requests to go? I tried to rewrite and place the entire path in the references, through str_replace(), but it’s no use, because javascript files request to the local server (localhost) and disrupt the operation in the same way. You can’t keep downloading everything and rewriting; I wanted to change this in the http header (I think it’s possible)

Youtube was just an example. I don’t want to copy the site but make modifications to the embed player’s css. (I don’t want libs, I’m doing this as an exercise)

Thank you all.

1 answer

2

The right term would be "turn relative url into absolute url".

Check if the URL has http at the beginning of the string. If it does not have a relative URL.

$domain = 'https://dominio/';
$url = '/pasta/arquivo.txt';

$url = ltrim($url, '/'); //remove barra inicial, caso exista.
if (substr($url, 0, 4) != 'http') {
    $url = $domain.$str; // concatena ao domínio
}

echo $url;

If you want, add more consistency as it is only checked if there is "http" at the beginning of the string but if you find an absolute URL with protocol other than "http" like "ftp://" for example, you will have an invalid URL.

One technique is to check whether within the first 12 characters it contains ://.

If there is :// and the beginning is different from http, ignore the entire URL or do another action you want.

  • You can also use parse_url and if you don’t find the host is a relative url.

  • I was talking about changing the browser’s understanding of the real host of the relative address. That way, I’d have to replace all the html, right? If so, it’s no good. But thanks anyway.

  • So it is not clear what you ask. Describe better if the question will not be closed. @inovapixel

  • @Danielomine summary: what I want is to make the browser understand that the address is related to another site that I want. I want to know if you can do this in the HTTP header, request header or Response, I don’t know. Got it? I want when the site is omitted from href or src, the browser to associate the "URL" to any site I want, and not the address bar...

  • Describe it in the question, but you’re still confused. What is this browser talk? Or I’m too slow to catch what I want to do or I’m really confused.. rsr Try drawing, make an illustration and things like that. O str_replace() nor is it necessary. Just prefix the domain with the Urls you find. I didn’t understand the section where you embed everything in Javascript and localhost. What the hell does that mean? rsrs Describe it in the question and not here in the comment because here you don’t have adequate space. @inovapixel

  • @Danielomine, from what I understand - maybe - whatever he wants to get close to the tag <base> for relative archives.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.