RelativeUrls

Summary: How to refer to files using relative URLs
Version: 1
Prerequisites:
Status:
Maintainer: Stefano
Categories: Uncategorized

Questions answered by this recipe

  • How can I avoid hardcoding links to images saved locally?
  • The images that are uploaded are saved in a directory named after the group's name - how can I refer to those files, without having to hardcode on the page where I show them the whole directory path?
  • I think that sooner or later I will change the name of my site - is there a way to avoid having hardcoded links in my pages?

Answer - Overview

  • First of all you have to edit your config.php file and define a variable which you will use later to refer to the path.
  • Second, on the page you edit, you mention the variable followed by the filename.

Answer - Detailed procedure

Edit your .../local/config.php file and enter the following line:
$FmtPV['<variable name>'] = "'<fixed part of the path>' . '{' . '\$Group' . '}/'";
After saving, you'll be able to get the path just by calling the variable you defined on your page with
{$<variable name>}

Example

If somebody who belongs to the group dummygroup uploads the file image.jpg to the host www.myhost.org, and pmwiki saves automatically the images to a directory called after the group's name (default behaviour) under the path /pmwiki/uploads, it will be enough to specify the following in config.php: $FmtPV['$Imagedir'] = "'http://www.myhost.org/pmwiki/uploads/' . '{' . '\$Group' . '}/'";

This way, on the page that that person edits, s/he will have to specify only {$Imagedir}image.jpg
in order to show the image on the page. Alternatively, if only the link has to be displayed, the whole can be enclosed as usual in double square brakets like this:
[[{$Imagedir}image.jpg]]
which will result in
[(approve links) edit diff]

Explanation

The string
$FmtPV['$Imagedir'] = "'http://www.myhost.org/pmwiki/uploads/' . '{' . '\$Group' . '}/'";
defines a variable called $Imagedir which will contain the path
http://www.myhost.org/pmwiki/uploads/{$Group}/
which is then translated with the real group name when it is loaded on the page using the variable
as http://www.myhost.org/pmwiki/uploads/dummygroup/

Notes

Release Notes

If the recipe has multiple releases, then release notes can be placed here. Note that it's often easier for people to work with "release dates" instead of "version numbers".

Comments

See Also

  • You can set in config.php the variable $UploadUrlFmt to a relative URL, for example:
    $UploadUrlFmt = '/pmwiki/uploads';
    Then your Attach: links will have relative URLs.
  • See $EnableLinkPageRelative for relative links to wiki pages.
  • PmWiki.InterMap allows you to define a relative (or not) URL prefix like Images:file.jpg from within the wiki.

Contributors

User notes? : If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki.