Relative links on pages without a trailing slash

On pages that don't have a forward slash in their url, is there a way to use relative links that will contain the page in the url?

For example link

a href = "content"

On the page

http://www.domain.com/page/

      

will refer to:

http://www.domain.com/page/content

      

But the same link on the page: (note the missing slash)

http://www.domain.com/page

      

will refer to:

http://www.domain.com/content

      

Since my site does not have a forward slash (second example), I cannot use relative links and must use the full path ( a href="/page/content"

).

+3


source to share


2 answers


Relative paths work relative to the folders where the html page is located.

So, for example, if you are in domain.com public_html and there is a folder named page. Then your link will link to domain.com/page/content.



Likewise if you were on a route like domain.com/page where your link was submitted as content. Clicking on this link will take you to the domain / page / content. Further reading: http://www.coffeecup.com/help/articles/absolute-vs-relative-pathslinks/

<a href="linkhere.html">Click Me</a>
This link points to a filename, with no path provided. This means that linkhere.html is located in the same folder as the page where this link appears.
If both files were located in the root directory of the Website http://www.website.com,
 the actual website address the user would be taken to is http://www.website.com/linkhere.html. 
If both files were located in a subfolder of the root directory called files, the user would be taken to http://www.website.com/files/linkhere.html.
How about another example? Let say we our http://www.website.com domain had a subfolder called pictures. Inside the pictures folder is a file called pictures.html. The full path to this page would be:
"http://www.website.com/pictures/pictures.html"
Still with us? Good. Let say in this pictures.html file, we have a link:
<a href="morepictures.html">More Pictures</a>
If someone clicked that, where do you think it would take them? 
If you said http://www.website.com/pictures/morepictures.html, you'd be right! 
You probably know why it would take them there: 
because both files are saved in the pictures subfolder.

      

0


source


In addition to including the trailing slash in the link in the first place, or redirecting from paths that lack the trailing slash, you can also set the base tag so that it always includes the trailing slash and the current path.

<head>
  <base href="/page/" target="_self">
</head>

      



Then relative links should work as expected if the browser supports it. It is also possible to completely define the path. You might not be able to install dynamically with javascript.

0


source







All Articles