Home > Mobile >  Make a relative url go one step back
Make a relative url go one step back

Time:07-12

I have a url: http://test.com/test1/test2/
Is there a way to make a relative url from test2 to test1? Like just one step back?
Without Javascript.
The /test1/ part may change, that's why I need to go from /test/2 just one step back to /test1/ (or to a changed url)

CodePudding user response:

You can link with ../. Indeed, if you visit /test1/test2/ and the URL contains ../, it will move one directory up, so /test1/.

This is specified in RFC-3986 of the W3 organization [w3.org], in the Merge paths section [w3.org], and as section 3.3 on Paths [w3.org] describes:

The path segments "." and "..", also known as dot-segments, are defined for relative reference within the path name hierarchy. They are intended for use at the beginning of a relative-path reference (Section 4.2) to indicate relative position within the hierarchical tree of names. This is similar to their role within some operating systems' file directory structures to indicate the current directory and parent directory, respectively.

You thus can make a link to go to the "parent directory" that looks like:

<a href="../">parent<a>
  • Related