I am retrieving a page from another host, and then initializing the form with data from a database before sending it on to the user.
I need to make the URLs in href
and src
attributes absolute, so that the browsers load them from the right place.
Can I set an HTTP header to cause this to happen without modifying the HTML?
YES or NO, depending on which HTTP spec you use.
Per HTML and URLs on W3C:
RFC 2068 is the original spec for HTTP 1.1. It defined
Content-Base
andContent-Location
headers for the purpose of specifying an entity's base URL used for resolving relative URLs within the entity:RFC 2068 is obsolete, replaced by RFC 2616, which is currently the most common HTTP 1.1 spec implemented by most web servers. It deletes the
Content-Base
header completely from the HTTP 1.1 spec, and slightly re-defines the semantics ofContent-Location
:It is important to note that "The value of Content-Location also defines the base URI for the entity" still applies at this point.
Moving forward, RFC 2616 has been obsoleted by RFCs 7230-7235 (which are not widely implemented yet). In particular, RFC 7231 completely redefines the semantics of
Content-Location
:Most importantly, RFC 7231 also states:
So, in answer to the question that was asked:
as of RFC 2616, the answer is YES,
Content-Location
exists to specify an entity's base URL at the HTTP level.as of RFC 7231, the answer is NO,
Content-Location
can no longer be used to specify an entity's base URL.AFAIK, as of RFC 7231, no new or existing HTTP header has been defined to restore the base URL behavior. So there is no longer an HTTP header available for specifying a base URL. It can only be specified by the entity itself, if it needs to be different than the entity's request URL.
There is no such for HTTP. But you can set the base URL with HTML’s
BASE
element like:No. The only way to do that would be a
<base>
element in the HTML output.See docs here: HTML
<base>
TagAlternative idea
if you can't touch the HTML, you should be able to put something together using
mod_rewrite
. You would build 301 redirect statements for your image resources, that will point forward to a remote server. The only condition for this is that your image requests follow a fixed pattern (e.g./images/xyz.jpg
) that you can translate into aRewriteRule
.Check out this tutorial to get you started.