user2806040
user2806040

Reputation:

Navigation with relative paths

I don't want to user absolute paths because I might change my domain name. How can I link to this location with a relative path?

https://[My Website Name].com/dir1/dir2/dir3/page.html

Or is this a bad idea?

Update: In other words, how can I load the home page (or other page) of my website with a relative path? I already use relative paths for accessing resources but I specifically want to know about navigation.

Upvotes: 1

Views: 2755

Answers (1)

Scott Marcus
Scott Marcus

Reputation: 65806

You should not be using absolute paths when linking to resources that are within your same domain in the first place. Sure, it will help if you change your domain name, but more importantly, if you use an absolute path, you will cause the user-agent to have to re-resolve the IP address of the domain, which takes time and wouldn't be necessary if you didn't.

When referencing resources that are outside of your site, you must use absolute paths (http://.....).

To use a relative path, you just need to know where the current file resides in your directory structure and understand (relatively speaking) where the destination resource is. Here are some rules you can follow:

If the destination resource is in the same directory as the source file, no path is needed - just put the name of the destination resource, i.e.: myFile.html

If the destination resource is in a sub-directory of the folder that the source file is in, just start the path with the directory name that is in the same folder as the source file and complete the path from there, i.e.: images/myimage.png

If the destination resource is in a higher level directory than the source, use ../ to navigate up to the parent directory of the source. For example, to get to a file one directory higher than where the current file is: ../myFile.html. If you need to go up more than one directory, you may combine multiples, i.e. ../../myFile.html would take you up two directories to find the file.

If the source is pretty far down in the overall site structure, it may be easier to start at the root of the site and work your way down, rather than going up several levels. In this case, begin the path with a / to indicate that you should start at the site's root. For example: /index.html would mean to go to the index.html file that sits in the root directory of the site.

Upvotes: 3

Related Questions