Last post Jan 23, 2020 03:32 AM by Yongqing Yu
Jan 22, 2020 05:34 PM|hypercode|LINK
It's been a while since I've posted. I've been out of web design for a couple years.
I have a question regarding relative URLs. Links on pages on my hosting server work when formatted as relative URLs as in the two methods below:
<a href="contacts.htm"> - without forward slash
<a href="/contacts.htm"> - with forward slash
As a rule of thumb, should the forward slash be there or not?
Thanks in advance for you help.
Jan 22, 2020 05:54 PM|Rion Williams|LINK
These should both resolve to the same canonical URL since
a single leading slash is considered by the spec to be an absolute URL, so it's likely going to be more a matter of preference:
A relative reference that begins with a single slash character is termed an absolute-path reference. A relative reference that does not begin with a slash character is termed a relative-path reference.
You can see a more in-depth discussion here, but you'll likely want to just ensure you are being consistent in whichever approach you decide to take.
Jan 23, 2020 03:32 AM|Yongqing Yu|LINK
Whether you need to add a forward slash depends entirely on whether your current page and contacts.htm page are in the same folder.
If they belong to the same folder side by side, then you do not need to add a forward slash, but if the two files are in different below the folder level, you need to modify the path of the href.
You can refer to this link for more details: HTML File Paths
If your files are in the same project, there is a quick way in vs to determine the path you need to reference the file,
you can directly drag the file you need (such as the contacts.htm page here) with the mouse move to your current front page, then it will automatically generate a relative path.