I have a page that has "domain relative URLs" such as ../file/foo
or /file/foo
, in a href
attributes, img src
attributes, video, audio, webcomponents, js ajax calls, etc.
Now here's the thing, when the page gets such URLs I want them to be relative to a single specific and different domain then it's actually on, so say the page is on http://localhost:8081/page/
the browser will translate them to:
http://localhost:8081/page/../file/foo
http://localhost:8081/file/foo
But what I really want is for all relative URLs to have no relationship to the domain that their on, but rather be in a relationship with another domain, ex http://localhost:5462/
, and therefor translate to:
http://localhost:5462/page/../file/foo
http://localhost:5462/file/foo
In other words: I want a page where the URLs it contains, in a-href, ajax calls, etc, never change no matter where you are viewing the page from (so that the page always fetches the same content from the same source), which works out of the box for complete URLs but not for relative URLs. A way to define and enforce a domain that URLs are relative to so that no matter where the page goes its content and behavior stays the same.
So, what options do I have to solve this and which is the best one? Some ideas:
a href
and img src
defined not using markdown but html, or various types of links inside webcomponent elements, etc (also, this isn't a very universal solution and doesn't work with js) meta
with options that I can put in the header of the html that changes the relationship between domain relative links/calls and the domain? I recently discovered the base
tag but it seems to only work with HTML elements and not js scripts. localhost:5462
behind localhost:8081
so that all the data from localhost:5462
is accessible at localhost:8081
, this is very clean and plainly just works , even for js http calls, and I will and already use this in allot of cases with zero problems, but sometimes I don't want to use routing and then I'm back to square one. So this is the result that I want, but I need it without routing as well (that the http requests from the frontend don't go through the backend but to the same URLs as on the original or "correct" domain). file:///
as well, which needs relative links, and how would I go about banning it in the first place and do I really want to ban a form of links? What seems like should be the best solution is something like the base tag but which also works for any arbitrary unmodified javascript, instead of just HTML elements.
But if that doesn't exist maybe it's possible to use the base
class just for HTML elements, and in conjunction with that use some very neat and non-hacky javascript that successfully intercepts all possible javascript and webassembly http (or any other protocol) requests and directs them to the correct domain if they are using domain relative URLs? And if that really is the best solution and technically feasible, how would I do it?
After experimenting a bit I've come to the conclusion that HTTP redirects are the best solution, essentially making the backend serve as nothing more but a way to bounce off of to the right URLs. There are some problems with POST requests though which might be possible to fix with frontend javascript modifications, but if it's really a problem it's always possible to switch redirects with "forwards" instead.
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.