Tricking a website to interpret root-relative paths as belonging to another domain?
I was wondering if it was possible to tell a server (or a browser) to interpret all root-relative paths (ie, href="/folder/file.htm" or src="/images/image.jpg") as belonging to a different domain?
Currently, if I'm on domain.com, the path "/folder/file.htm" will be interpreted by the browser as "http://domain.com/folder/file.htm". But can the code be tricked into pointing all those links on a different domain than the one the HTML file is on? Something universal that would encompass ALL path references, even the paths located in the .js files... without actually modifying them in the code (hard-coding "http://alernatedomain.com" as a prefix to all those paths via search/replace is the long and clumsy way of achieving the same result, but I'd rather leave the bulk of the code unmodified).
I won't bore you with a detailed explanation of my reasons for asking this, but I'm essentially hot-linking everything between two domains I own. One domain has the HTML file only, while another has everything else from stylesheets to scripts to images to other HTML pages to link to. Since I own both, there's no danger of one side shutting down hot-linking to the other.
There are some good reasons to keep some resources on a second domain (stylesheets and other common graphic elements), but its best to hard-code the domain names if you do.
Thanks, I appreciate the warnings, they come from a good faith place... but I'd really just rather learn about how to do this. If for no other reason than my personal knowledge.
in your <head>,
<base href="http://domain.com/" />
there is no way in the world that 5% of folks have no JS in their browser.
Originally Posted by rtrethewey
MAYBE 7-10 years ago that was the case, but it's a lot harder to 1. disable it and 2. use any good site without it.
Wait, could it possibly be this simple? This is awesome, if it really works! Any compatibility issues, or is it supported as widely as standard HTML?
Originally Posted by rnd me
Also, what happens to full http paths that are mixed in with the root-relative ones? Your solution seems to transform src="/image.jpg" into src="domain.com/image.jpg" in the eyes of the browser, but what happens to src="http://domain.com/image.jpg" in the same context? Does it become src="http://domain.com/domain.com/image.jpg", or is the code smart enough to ignore full-path urls and affect only the root-relative ones?
Having tried the <base> method, I can confirm that it appears to work near-flawlessly. I say "near", because the only downside I can see is that Adobe Dreamweaver doesn't seem to recognize its meaning, so the preview screen looks nothing like it's supposed to. Not a very big deal, since (in my case) this will typically be the last step of the web design process... but it was worth mentioning nonetheless.
If you use the <base> tag, you'll need to use complete URLs for all of your <a>nchor tag 'href' values. If you use relative URLs, they'll also be converted to point to the domain in the <base> tag and your site navigation will fail.
Originally Posted by rtrethewey
Believe me when I say the average person doesn't even know how to turn it off, much less have any inkling of why they would want to do so in the first place.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)