Alternatives to base tag - html

I used mode rewrite on my website
I used the base tag to solve my relative links problem
<base href="/" />
But the problem is absolute link eg. http://www.absolutelinks.com
It changes it to www.mysite.com/http://www.absolutelinks.com
How can i fix this

Base href applies only to the relative URL so if you have got: Google you'll be redirected to Google, not http://mydomain/http://google.com/. Please post the code of your HTML document.
However using base isn't the best practice. Much better approach is to use absolute URLs like: src="/styles/main.css" which always points to mydomain/styles/main.css.

Don't use <base> at all, instead have some server-side config and keep a $base variable there - then, when outputting any URL during your HTML generation use {$base}{$restofurl}.
This works well when you have the same code running in development/test/live environments - you just need to change your server-side $base config.
Using PHP/Smarty syntax above but I'm sure you get the idea.

Related

PHP and html how do i link to an external URL?

consider the following code:
<a id="alink" href="http://google.com">google</a>
This is a fairly basic link tag. At the top of my html page I have:
<base href="//localhost/website/" />
This creates a problem, when i click my link it brings me to:
//localhost/website/http://google.com
I do not want this, I want it to bring me to a completely different site(google.com for example). How can I fix this problem?
try this one.
<base href="http://localhost/website/" />
on localhost no need for directory or double slashes // !=link.
The effect of the base tag is global to the document, and the only way to override the effect of  is to use absolute URLs.
You can use window.location in JavaScript to get the URL of the page itself, in case the document was retrieved via HTTP. And you could use it to construct absolute URLs.
But It is better to use server-side technologies that let you construct addresses from one or more base addresses. So quite possibly the best approach is to get rid of the tag.

Change all links in a specific web app from root (absolute) to add a subfolder?

I am deploying an app that is designed to run on the root and the html pages have a lot of links in the form:
/something/file.ext
like
/img/logo.png
/css/main.css
/js/app.js
and links too:
/link/to/url
I need to change them all into:
/subfolder/link/to/url
Is there any elegant way to do this without going page by page and changing it by hand?
I used Apache RewriteBase and HTML's base element..
I also read this question and answer which suggests what I'm doing should work:
Change BASE HREF for absolute references?
But it does not work!
I am doing this:
<base href="http://somesitename.com/subfoldername">
What happens is that the links still go to http://somesitename.com/url instead of the desired result.
The best solution I was able to come up with is the following:
Mass search and replace of
<head>
With:
<head><base href="http://newsite.com/newfolder">
and then search and replace
"/ or '/
with
"
Your milage may vary. I also had to replace some urls inside the javascript code.
Anything that started with a forward slash.

Why Does this relative URL work?

So I've been playing with writing some web crawlers and testing them on different sites. But I've come across some sites that seem like their relative urls should not work, or at least I think they should point to someplace other than where the browser resolves them to.
Given a url of a current page : "http://www.examplesite.com/a/page.htm"
And a link of: "a/page2.htm"
The browser correctly resolves this as: "http://www.examplesite.com/a/page2.htm"
My problem/feeling (obviously wrong, but I'm wondering why) is that this should resolve to "http://www.examplesite.com/a/a/page2.htm". The relative url does not begin with a /, so why does it become base relative?
Interestingly, Java's URL class appears to agree with me, as the following code will output : "http://www.examplesite.com/a/a/page2.htm"
URL baseUrl = new URL("http://www.examplesite.com/a/page.htm");
URL absoluteURL = new URL(baseURL,"a/page2.htm");
Why does this link resolve the way it does, and what is the formal rule for resolving a relative link like this?
EDIT:
I just notice that in the <head> portion of the webpage there is a field like so:
<base href="http://examplesite.com/">
I'm assuming that this overrides any relative links to use that as its base url instead of the actual url. Is this a correct assumption? Is that even a valid html markup?
You are correct in that it is the base tag, and yes it is valid.
In HTML, links and references to external images, applets,
form-processing programs, style sheets, etc. are always specified by a
URI. Relative URIs are resolved according to a base URI, which may
come from a variety of sources. The BASE element allows authors to
specify a document's base URI explicitly.
When present, the BASE element must appear in the HEAD section of an
HTML document, before any element that refers to an external source.
The path information specified by the BASE element only affects URIs
in the document where the element appears.
Sources: W3C Wiki and W3C Markup
The site is likely using a <base> tag to specify the parent as the prefix to all relative URL's on the site.
You can find out more on the base tag here. If this is not the case, then please provide the source URL as this defies normal behavior.

Is it OK to use empty href on links with <base> tag

I set the base tag as such:
<base href="http://mnapoli.github.com/PHP-DI/">
Then I would like to create a link to http://mnapoli.github.com/PHP-DI/ in relative path.
I tried:
link
and it works in Chrome, but is this method standard and supposed to work on every browser?
Although href="./" as suggested in Mike’s answer is better (easier to understand to anyone who reads the code), the answer to the question posed is that using an empty URL is standard and supposed to work on all browsers. According to STD 66, a relative URL with no characters (path-empty) is allowed, and by rules on relative URLs, it is resolved as the base URL.
This has nothing to do with folders or files; URLs are strings, and whether they get mapped to folders or files on a server is at the discretion of the server.
I would do something like this:
<base href="http://mnapoli.github.com/PHP-DI/">
Home

How to change the root for links in html

I was hoping that adding a <base href="http://www.myweb.com/newroot/" /> tag would cause that links like /link would point to http://www.myweb.com/newroot/link instead of http://www.myweb.com/link.
Soon I realized it does not work that way and it only applies to hrefs which do not start with /.
But is there any way to achieve the behavior I expected before (without the use of JavaScript)?
links with a leading / always point to the root/domain. This is a client-side issue, i think there is no other solution than dropping the / of using js.
You can use the HTML base element like you did. but instead to linking to /link try simply linking to link