Thirteen
Thirteen

Reputation: 13

Do I need to specify a webpage's url?

I've uploaded several files to my server and it's really quite baffling. The home page is saved as index.html, and when I type in the URL of said page it miraculously, and quite successfully shows the right page. What about my other pages? I have linked to them from the home page with the following code:

<a href="http://www.example.com/about/">About Us</a>

How does my html file, presumably called about.html, supposed to know that its URL is "http://www.example.com/about/"? I am dubbing this "The Unanswered Question" because I have looked at numerous examples of metadata and there is nothing about specifying the URL of a page.

Upvotes: 1

Views: 872

Answers (6)

Nicole
Nicole

Reputation: 33197

It depends on what type of server you are running.

Static web servers

If it is the simplest kind of static file server with no URL aliasing or rewriting then URLs will map directly to files:

If your "web root" was /home/youruser/www/, then that means:

http://www.example.com -> /home/youruser/www/

And any paths (everything after the domain name) translate directly to paths under that web root:

http://www.example.com/about.html -> /home/youruser/www/about.html

Usually web servers will look automatically for an "index.html" file if no file is specified (i.e. the URL ends in a /):

http://www.example.com/ -> /home/youruser/www/index.html
http://www.example.com/about/ -> /home/youruser/www/about/index.html

In Apache, the filename searched for is configurable with the DirectoryIndex directive:

DirectoryIndex index.html index.txt /cgi-bin/index.pl

That means that every request to a path that ends in a / (and to add yet another rule, under some common settings it will automatically append a / if the path is the name of a directory, for example 'about'):

http://www.example.com/ -> /home/youruser/www/index.html
                        -> or /home/youruser/www/index.txt
                        -> or /home/youruser/www/cgi-bin/index.pl

Web servers with path interpretation

There are too many different types of servers which perform this functionality to list them all, but the basic idea is that a request to the server is captured by a program and then the program decides what to output based on the path.

For example, a program might perform different routes for basic matching rules:

*.(gif|jpg|css|js) -> look for and return the file from /home/user/static
blog/*             -> send to a "blog" program to generate the resulting page
                      using a combination of templates and database resources

Examples include:

  • Python
  • Java Servlets
  • Apache mod_rewrites (used by Wordpress, etc.)

Links in HTML pages

Finally, the links in the HTML pages just change the URL of the location bar. The behavior of an HTML link is the same regardless of what exists on the server. And the server, in turn, only responds to HTTP requests and only produces resources (HTML, images, CSS, JavaScript, etc.), which your browser consumes. The server only serves those resources and does not have any special behavioral link with them.

  • Absolute URLs are those that start with a scheme (such as http: as you have done). The whole content of the location bar will be replaced with this when the user clicks the link.
  • Domain relative URLs are those that start with a forward slash (/). Everything after the domain name will be replaced with the contents of this link.
  • Relative URLs are everything else. Everything after the last directory (/) in the URL will be replaced with the contents of this link.

Examples:

  • My page on "mydomain.com" can link to your site using the <a href="http://www.example.com/about/">Example.com about</a> just as you have done.
  • If I change my links to <a href="/about">about</a> then it will link to mydomain.com instead.

An answer your question

How does my html file, presumably called about.html, supposed to know that its URL is "http://www.example.com/about/"?

First, the file itself has no idea what its URL is. Unless:

  • the HTML was dynamically generated using a program. Most server-side languages provide a way to get this.
  • after the page is served, client-side scripts can also detect the current URL

Second, if the URL is /about and the file is actually about.html then you probably have some kind of rewriting going on. Remember that paths, in their simplest, are literal translations and /about is not the same as about.html.

Upvotes: 3

leigero
leigero

Reputation: 3283

about.html doesn't know what it's URL is, its the index.html file that calls your about.html file.

When you're in any given directory, linking to other pages within that directory is done via a simple relative link:

<a href="/about.html">About Us</a>

Moving up a directory, assuming you're in a sub folder (users) perhaps you can use the .. operator to navigate up one directory:

<a href="../about.html">About Us</a>

In your case your about page is in the same directory as the page you're linking from so it just goes to the right page.

Additionally (and I think this may be what you're asking) if you have:

about.html
about.php
about.phtml
about.jpg

for example, and you visit http://www.yoursite.com/about it will automatically bring up the html page and the other pages should be referenced explicitly somewhere if you want them to be used.

Upvotes: 0

Xarcell
Xarcell

Reputation: 2011

I'm a bit confused, but here is some information. Any file named "index" is the default display page for any directory(folder) when trying to view that directory.

All files in a folder are always relative to that directory. So if your link is in a file, within a different directory, then you must type in that directory along with the file. If it is the same directory, then there is no need to type in that directory, just the file name.

Upvotes: 0

federicot
federicot

Reputation: 12341

Theoretically, it's better for URLs in your documents to be relative, so that you don't have to change them in the event you change the domain or the files location.

For example, if you move it from localhost to your hosted server.

In your example, instead of www.example.com/about.html use /about.html.

Upvotes: 1

happyhuman
happyhuman

Reputation: 294

Given the link above you would need a about page named index.html located in a directory named about for your example to work. That is however not common practice.

Upvotes: 0

samdonly1
samdonly1

Reputation: 438

Just use /about.html to link to the page

Upvotes: 1

Related Questions