简体   繁体   中英

php get_contents - Keep original url for source files?

I need to display content from an external page. This is my code:

<?php
$homepage = file_get_contents('http://www.domain.com');
 echo $homepage;
?>

It works, but all the paths/links for images, links and js files are broken. How do I do this, while keeping the original links? When I click "view page source", it shows the images as

<img src="images/blah.jpg" />

Instead, it needs to show the orginal domain name, like this:

<img src="http://www.domain.com/images/blah.jpg" />

It needs to do this for all links, javascript files and images.

The problem is that the page is using relative links, so it is referring to itself using the domain name that you are hosting as opposed to the domain that you are taking the content from.

The right thing to do would be to redirect the user using the header function, which will also send the correct 302 HTTP code

header('Location: http://www.domain.com/');

Your only options that I can tell are to find and rewrite any tags that do not have a domain, but that is troublesome because you are going to miss any that are generated on the fly. An alternative, but more devious and wrong, is to write an iframe which is 100% high and wide, and present the page there as coming from your domain. But that seems a little rude unless it is actually your site.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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