3

Sorry if it so basic but I could not find the answer by searching.

If we are in the page http://www.example.com/a-dir-without-trailing-slash how we can reach the sub-directory http://www.example.com/a-dir-without-trailing-slash/pic using relative URI? (we do not know the current directory name(i.e. a-dir-without-trailing-slash)

Some more explanation:

a-dir-without-trailing-slash is the name of an article in the website. It is not an actual directory nor an actual file name. Now, I want to get the pictures that are used in this article by addresses like:

http://www.example.com/a-dir-without-trailing-slash/pic/1

http://www.example.com/a-dir-without-trailing-slash/pic/2

,...

and in the webpage html, I would refer to them with something similar to:

 <img src="pic/1" />

If the original article address was in the form of http://www.example.com/a-dir-with-trailing-slash/, the above example would work finely. I want to know if is it possible to get a relative URI with current article addresses (without trailing slash)?

Thank you very much

Ormoz
  • 2,975
  • 10
  • 35
  • 50
  • 2
    You can't. Relative URLs are interpreted relative to the directory portion of the current URL, the filename part is not available. – Barmar Jan 02 '15 at 05:40
  • 1
    You need server side scripting to add the filename to urls (or may be just one `` tag in the head). – Salman A Jan 08 '15 at 21:37
  • 1
    @SalmanA Thank you very much, the `base` tag solved my problem. I would be happy to give you the bounty, If you re-write your answer in a question. Thank you again. – Ormoz Jan 09 '15 at 15:11

2 Answers2

4

I suppose you want to avoid hard coding "slugs" in the content so that they can be stored and manipulated independent of each other.

One solution is to use the base tag which allows you to specify the prefix that is added to relative URLs instead of typing them all over the place.

  1. Make sure that your website uses absolute URLs where necessary.
  2. Modify your CMS to "generate" and place the following tag inside the head section that contains trailing slash:
<base href="/a-dir-without-trailing-slash/">
  1. Then you can use relative URLs inside the content, for example:
<img src="pic/1">
<!-- http://www.example.com/a-dir-without-trailing-slash.html/pic/1 -->
Salman A
  • 262,204
  • 82
  • 430
  • 521
-1

You need server side scripting to add the filename to urls (or may be just one '> tag in the head). – Salman Bounty get.

  • None of them work. both will assume that `a-dir-without-trailing-slash` is a file name so they both refer to `http://www.example.com/pic/1` – Ormoz Jan 04 '15 at 22:22
  • I want to use relative uri in the `src` of image. so when I put `pic/`` in the `src`, it shout refer to the first pic of the article not the first pic of the parent – Ormoz Jan 04 '15 at 22:28
  • Thanks, But the problem is that the address is a virtual directory not a real one( this address comes from database and actually does not exits) – Ormoz Jan 05 '15 at 04:20