Linking to a external Url - currently unsuccessful - html

When I try link a ext Url such as www.facebook.com it currently comes as -
mysite.com/music/www.facebook.com
Its linked: <a href="www.facebook.com></a> as i link internal urls like <a href=./index.html> etc etc
How to make it work?

I assume you're talking about in HTML. You need the full URL, e.g. http://www.example.com, not just www.example.com.

Try adding an http:// prefix to your URLs: http://stackoverflow.com/faq
You might find the HTML 4.01 specification interesting reading. (Probably not. But pay attention to the 12.4.1 Relative URIs section at the bottom of the page. :)

Related

href to subdirectory of current url

Assume the current url is : www.my.com/somewhere.
And I have a href link and want it go to : www.my.com/somewhere/subpage without writing current url(www.my.com/somewhere) in href.
Is it possible in pure HTML (not by javascript or server-side things)?
2020 - current state of this question:
if the current URL is this www.my.com/somewhere
The shorthand ./subpage or just subpage will navigate to www.my.com/subpage
If the current URL ends in a /, then it will navigate as question is asking.
www.my.com/somewhere is not the same as www.my.com/somewhere/
So how to navigate to subpage from www.my.com/somewhere?
use: somewhere/subpage
No, a relative link is relative to your current directory and domain.com/somewhere is a file, while domain.com/somewhere/ is a directory.
Since /somewhere is a file, the current directory becomes '/' and a relative link to 'subpage' becomes a link to 'domain.com/subpage'.
If you used directories for your pages instead of files, then '/somewhere/' would become your current directory. A link to 'subpage/' would now be relative to '/somewhere/' and would open 'www.mydomain.com/somewhere/subpage/'. But this would probably require 'server-side things', and therefor: No, I don't think it's possible to link to a subpage of the current document.
I think ./subpage should do the job.
You can simply write the relative path in your href, like this:
<a href='subpage'></a>
Unless I am misunderstanding you. You should just be able to write href="/subpage".

Is it bad practise to start links with "/" in html?

My website code sample:
<a href=/post/64/page-name><img src=/img-folder/2015/09/image.jpg></a>
<div id=cont2><a href=/post/64/page2>page 2 link</a></div>
My first question is, can I start links just with /? Is it a bad practise? because all website sources that I looked it starts with www.website.com/... not just /
Second question is quotes. It is not needed since html 2.0, but is it important in the example above?
My website is having some problems on google to show correctly... may it be because this problems?
It isn't bad practice. A URL starting with / is merely a relative URL that's relative the the base path. You're using it just fine.
Another example usage is when you want to reference a CSS or JavaScript file and you're deep down into the path.
<script src="/scripts/main.js"></script>
Then, no matter where the user is on your site, they'd always request http://example.com/scripts/main.js. Where example.com is your site's domain.
Additionally: Always quote attribute values. (attribute="value" and not attribute=value).
/ means start of where you are currently. So if your resource is located under same directory, you are allowed to use / to start with. If you refer to external resources, you can't use / to start. (E.g. www.google.com means google website, while /www.google.com means folder under your current directory named www.google.com, like http://localhost/www.google.com)
Quotes are needed when you use white-space in your attributes values (e.g. class="my super classs-name that has white-space" | class=my super classs-name that has white-space).
My website is having some problems on google to show correctly SEO stuff. What problems? Your page is not under first page of Google search? It's separate topic about that.
It is not forbidden. When you start your link with slash / it just a path relative to base element.
You can read more about BASE element here: http://www.w3.org/TR/html4/struct/links.html#h-12.4
For example, if you are already at: http://example.com/folder/index.html
/posts/index.html would link to: http://www.example.com/posts/index.html
posts/idnex.html would link to http://www.example.com/folder/posts/index.html
if you reference external sources you have to add the full path/adress
if you reference local resources its up to you.(more or less) take a llok at How to properly reference local resources in HTML?
You should use either double " or single ' qoutations - thats a good practise at least.
But you dont have to if there is no whitespace.
When you start your link with "/" its mean that you start from the root directory.
Example: Your website is in the directory /web/html.
When you now start your link with "/" its goes to the root folder. In this case the web folder.
I know this is old and answered, but it came up on Google when I was searching for something similar, so I just wanted to add to the answers.
Sometimes, when I need to do something real quick with simple HTML site that doesn't require a server, I usually just open index.html in Terminal to quickly preview the page in browser. However when you open your site like that, using the leading slash to load resources (ie. /js/main.js) won't work. That's because when you load your website by opening a file in your browser, the browser takes the root of your drive as the base path for your website.
So if you have your files like this for instance:
drive/Users/username/Documents/www/index.html
drive/Users/username/Documents/www/js/main.js
And you reference your script like this:
<script src="/js/main.js"></script>
The browser will think you're actually pointing here (if you open the file directly in browser):
drive/js/main.js
Because / in this case means drive and not the website's root (www in this case) folder as it would on a server.
Nope, it is not a bad habit to put '/' when starting links. But not having a quote in every html attribute? I don't think so. But i would suggest to put quote(") in every html attribute for it to be more readable.

How does an anchor tag work that does not specify a filename?

How does an anchor tag work that does not specify a filename?
Example:
Fast Cars
I was under the impression the HTML file had to be expressly cited, but in this case it is not, just the directory name.
Does this reference an HTML file, or is it something else?
Depending on the webserver configuration, pointing to a directory might mean pointing to the index file of that folder, or even at whatever other file you want to rewrite.
There was a time in which SEO practices reccomended using folder style permalinks instead of query strings. So there were cases in which
/index.php?section=fastcars
was rewritten as
/fastcars
Again, this is all rewrite magic. Most webservers offer some kind of rewrite rules to achieve this.
Besides, an a element doesn't need to point to a file or url address. They can be used as internal navigation links to point to an anchor, and JS frameworks such as jQuery have popularized its use only as button replacements to trigger a given behavior.
An anchor tag's href attribute does not reference a file; it references a URL. What that URI corresponds to is completely opaque. In the most common case where the URL uses the HTTP(S) protocol, that URL is sent to the server as part of a request.
It's the server, and only the server, that determines what the URL corresponds to. It doesn't have to be a file or anything else that you might consider tangible, and you can't tell looking at it from the client side.
The main function of the "a" attribute is to create links. That means a link to an external website:
<a href="https://www.stackoverflow.com">
or a link within the same page. Just create an id - for example:
<h2 id="top">text text text</h2>
then create a link using that id -
Top
Every time you click on the link, your browser will send you to the part of the page where your id is.

referring to .css and images from script-generated HTML

I have a site with static HTML pages in the home directory. These HTML pages use relative paths to refer to images, css, and links i.e.
<img src="images/myimg.gif">
and
Contact Us
I also have a monolithic script whose URL is, i.e. http://mysite.com/myScript which uses "extra path info" to select functions... i.e. http://mysite.com/myScript/products shows a list of products. So in HTML generated from the script I need to refer to images, css and links like this:
<img src="../images/myimg.gif">
and
Contact Us
The problem is now I want to start moving common HTML into include files, (e.g. common header and footer), but the fact that the script and the static HTML refer to relative resources in different ways is complicating matters.
I don't want to use absolute paths because that messes up my colleague's work when she tries to work on the pages in DramWeaver, and it also makes the site less flexible.
What's the best way to solve this issue? One idea I had was to use URL rewriting in Apache to allow the URL to http://mysite.com/products to really use http://mysite.com/myScript/products but I don't have experience with URL rewriting so I don't know how easy that would be. Another idea I had was to use the META BASE attribute in HTML but I don't like the fact that I would have to hard-code that into every HTML page, and it would have to have the full URL hard-coded (e.g. http://mysite.com/) into each one. Any advice?
Can't you refer to your images with a slash at the beginning so all files linked to are from the root, no matter how deep you are in the directory structure you are? E.g:
<img src="/images/myimg.gif" />
EDIT:
You could use $_SERVER to get the path then use substr_count to count the number of slashes in the path. Add as many ../'s as you need based on that number. Would that work for you?

Base URL for HTML and CSS

I got a question and although I could find related information, I'm if it was exactly about what I'm wondering about.
The thing is, I got a site on http://localhost/site.
Now, when I create a link, let's say, <a href="/posts">, it links to http://localhost/posts instead of http://localhost/site/posts.
It works fine if I remove the slash (<a href="posts">), that would be the closest and maybe the easiest solution, but I'd like to know why the links ignore the folder where the file is at?
And I also would like to know if this can be fixed with .htaccess or something.
I've read that a link that begins with / makes it "absolute". So a link beginning with / is only intended to be used to link directly to the root, or to be used by sites stored at the root (in this case it wouldn't make much sense?) ?
The leading '/' at the start of the URL informs the web browser that the path given is absolute (with respect to the web server root), i.e. if you link to /posts then you know that the resulting link will be to http://www.mysite.com/posts.
If you don't supply the leading '/' (and you don't give a complete url like http://www.mysite.com/posts) then usually the url is relative, and any page given will be relatvie to the page currently being viewed.
For example:
page being viewed link url target page
------------------------------------------------------------------------------
www.mysite.com/site link.html www.mysite.com/site/link.html
www.mysite.com/site ../link.html www.mysite.com/link.html
www.mysite.com/some/other/page link.html www.mysite.com/some/other/page/link.html
www.mysite.com/some/other/page ../../../link.html www.mysite.com/link.html
The decision on whether to use absolute or relative links is entirely up to you - the advantage of relative links is that if your site moves, links between pages on your site will still work correctly (for example if your site moves to www.mysite.com/otherpath, then any absolute links such www.mysite.com/originalpath/home will no longer work.
You should see the following site for a more complete explanation of relative urls:
Relative URLs (WebReference.com)
Your site root is localhost although you assume that site is your site root. When you use / it is relative to localhost as it is an absolute link.
Try doing it < a href="../posts" >
./ Means base directory, or home
../ Means one directory up