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

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.

Related

How to go to sub-directory with relative URI

Sorry if it so basic but I could not find the answer by searching.
If we are in the page http://www.example.com/a-dir-without-trailing-slash how we can reach the sub-directory http://www.example.com/a-dir-without-trailing-slash/pic using relative URI? (we do not know the current directory name(i.e. a-dir-without-trailing-slash)
Some more explanation:
a-dir-without-trailing-slash is the name of an article in the website. It is not an actual directory nor an actual file name. Now, I want to get the pictures that are used in this article by addresses like:
http://www.example.com/a-dir-without-trailing-slash/pic/1
http://www.example.com/a-dir-without-trailing-slash/pic/2
,...
and in the webpage html, I would refer to them with something similar to:
<img src="pic/1" />
If the original article address was in the form of http://www.example.com/a-dir-with-trailing-slash/, the above example would work finely. I want to know if is it possible to get a relative URI with current article addresses (without trailing slash)?
Thank you very much
I suppose you want to avoid hard coding "slugs" in the content so that they can be stored and manipulated independent of each other.
One solution is to use the base tag which allows you to specify the prefix that is added to relative URLs instead of typing them all over the place.
Make sure that your website uses absolute URLs where necessary.
Modify your CMS to "generate" and place the following tag inside the head section that contains trailing slash:
<base href="/a-dir-without-trailing-slash/">
Then you can use relative URLs inside the content, for example:
<img src="pic/1">
<!-- http://www.example.com/a-dir-without-trailing-slash.html/pic/1 -->
You need server side scripting to add the filename to urls (or may be just one '> tag in the head). – Salman
Bounty get.

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.

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

Overiding/Appending querystrings

Just a curiousity rather than an acutal need, I've never thought about this, but I can't think of any html which would do this. I could do it in Javascript or Serverside easy enough, but curious if browers can implement this anyway.
Say I'm at a url: http://www.mysite.com/?param1=10
and I have an <a> tag, is there anyway to make it's href so that it will append a new parameter to the qs?
So I could have <a href='?param2=20'>Twenty</a> which would make the url http://www.mysite.com/?param2=20. But I want it to be http://www.mysite.com/?param1=10&param2=20. Conversely if I was on http://www.mysite.com/?param1=10&param2=20 and there was a link to turn the url into http://www.mysite.com/?param1=30&param2=20, by only specifiying the param1 parameter in the tag? <a href='?param1=20'>10</a>.
If that makes sense? So can this be done just with html, no js or serverside, and not form submissions, just <a> tags?
Short answer: no. HTML is not a programming language, just a markup language.
No, it can't be done. Links can be relative to the site (with leading slash):
link
or to the page (no slash)
link
they can lead to a local anchor ( with a #)
link
or they can be absolute
link
or of course combinations of the above with local anchors.
But they can't append to the query string.

Alternatives to base tag

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.