2

Im on the page:

example.com/news

It's a list of news articles. Is it possible to set links on each news article and take into account the current url?

<a href="the-article">link to something</a>

The above will link to:

example.com/news/article

Or will I need to get the entire route and specify that in the link?

The url could be anything eg. /products so I do not want to hardcode it in.

shennan
  • 10,798
  • 5
  • 44
  • 79
panthro
  • 22,779
  • 66
  • 183
  • 324

3 Answers3

1

If you need to take into account the current path, use the page name directly in the href attribute:

If you are on example.com/news and used an href value of "article", the URL becomes example.com/news/article.

If you need to reference pages on the root directory, precede the page name with slash '/', href="/article".

Oday Fraiwan
  • 1,147
  • 1
  • 9
  • 21
1

Browser replaces the part comes after the last / of open page's URL to produce absolute URL from relative path. Either:

  • Use filenames at the end of your URLs (like news/index.html)
  • Use / at the end of your URLs (like news/)
  • Use absolute URLs instead of relative paths for href (like example.com/news/the-article)
  • Set a base element to guide the browser to add last /
    <base href="http://example.com/news/the-article/" />
    
ufukty
  • 312
  • 3
  • 10
0

Make it relative?

<a href="./article">link to something</a>

For some browsers/DOCTYPE, you may have to use this in conjunction with the base tag element, which will need to be added to every page that utilises relative paths:

<base href="http://www.example.com/news">
shennan
  • 10,798
  • 5
  • 44
  • 79