Absolute Paths and Relative Paths
Absolute Paths and Relative Paths
When conducting web development or file management, there are two methods for specifying the location of links and files: "absolute paths" and "relative paths." Each has different uses and characteristics, and using them appropriately in the correct situations enables efficient work. This article explains the differences between absolute and relative paths, and introduces the merits and demerits of each, as well as appropriate usage scenarios.
Absolute Paths
An absolute path is a method of describing the location of a file or page starting from the highest level of the hierarchy. With this method, you can access the specified page from anywhere.
Example
Absolute path of Page A: https://x.ptengine.jp/aaa/bbb/1.html
By using this format, you can access the above URL from any environment.
Merits
Since the URL is completely specified, the possibility of broken links is low.
Demerits
Because the URL contains "http://~", testing in a local environment is not possible.
Relative Paths
A relative path is a method of describing the relative positional relationship based on the current location. You specify the necessary elements in order to indicate the target point.
Example
Relative path: ../aaa/bbb/1.html
This notation indicates the location of the file based on the current folder. However, this path does not necessarily belong to a specific domain (for example: https://x.ptengine.jp). The same path may exist on another domain.
Merits
Works correctly even in a local environment, making testing easy.
The notation is concise, improving work efficiency.
Demerits
Broken links may occur due to page movement or structural changes.
Proper Usage
When Absolute Paths Are Appropriate
When setting external links: When you need to share links between different domains or servers.
When you have a fixed link structure: On the premise that the link structure of the entire site will not change.
When you want to provide accurate links to clients or external users.
When Relative Paths Are Appropriate
Development and testing in a local environment: Since it works in a local environment, testing is easy.
When setting links within the same domain: By keeping the notation concise, work efficiency improves.
When setting in-page links (anchor links): Easy to realize transitions to specific sections. For example, when you want to transition to a specific location on the page by clicking a button, you can complete the setup by adding a unique #id to the location you want to transition to, selecting a relative path in the button link, and entering the #id. For specific setup methods, please refer to here.
最終更新