Absolute vs Relative Path
implement full support for relative path1. Objectives
- add support for relative path
- option to set only absolute path, enforce canonical URL
- option to auto set relative path/absolute URL
- always adjusts to current environment
- to run under multiple domains
- always adjusts to current environment
Patches
- added option to switch between absolute and relative links[link1]
- set canonical context for feeds[link2]
impediments
- parse_url & unicode
absolute path must ensured for
- cookiesURL
- redirect
- canonical
- sitemap
- XML feeds
- email content
| Types | Example |
|---|---|
| absolute URL | https://example.com/folder/image.png |
| protocol-relative URL | //example.com/folder/image.png |
| root-relative URL | /folder/image.png |
| base-relative URL | folder/image.png |
2. Config
Ifcanonical is not enforced, base_path is applied where possible, all other cases use always base_url.- base_url (absolute)
- base_path (relative)
- base_path (canonical ? base_url : base_path)
<?php
'base_url' => ($_SERVER['SERVER_PORT'] == 443
? 'https'
: 'http'
) . '://' .
$_SERVER['SERVER_NAME'] .
(!in_array($_SERVER['SERVER_PORT'], [80, 443])
? ':' . $_SERVER['SERVER_PORT']
: ''
) .
(($path = preg_replace('/\/\//', '\/', trim(strtr(dirname($_SERVER['SCRIPT_NAME']), '\\', '/'), '/')))
? '/' . $path
: ''
) . '/' , 2.1. Get base URL
<?php
// returns the full absolute or relative URL to the directory where WackoWiki is installed
function get_base_url($absolute = null)
{
$base_url = ($_SERVER['SERVER_PORT'] == 443
? 'https'
: 'http'
) . '://' .
$_SERVER['SERVER_NAME'] .
(!in_array($_SERVER['SERVER_PORT'], [80, 443])
? ':' . $_SERVER['SERVER_PORT']
: ''
);
$base_path = (($path = preg_replace('/\/\//', '\/', trim(strtr(dirname($_SERVER['SCRIPT_NAME']), '\\', '/'), '/')))
? '/' . $path
: ''
) . '/';
return ($absolute ? $base_url : '') . $base_path;
} 3. Relative URLs
<a href="/wiki/">'base_url' => '/wiki/',
4. Absolute URLs
<a href="https://example.com/wiki/">'base_url' => 'http://example.com/wiki/',
$base_url = 'http[s]://example.com/wiki/'; $base_path = '/wiki/';
5. Canonical context
It sets canonical context for feeds, this solves three issues:-
format($page['body_r'], 'post_wacko')-> renders content with canonical links -
href()-> sets link absolute -
link()-> sets path for global files absolute
6. Resources
- RFC 2396 - Uniform Resource Identifiers (URI): Generic Syntax[link3]
- Absolute vs relative URLs[link4]
- [link1] https://codeberg.org/WackoWiki/wackowiki/commit/a7fb41de84d9310061c3ca5d7a672c902a6f8b31
- [link2] https://codeberg.org/WackoWiki/wackowiki/commit/4363763ec9f01f6226a9782f6eda94bc08701cc7
- [link3] https://tools.ietf.org/html/rfc2396
- [link4] https://stackoverflow.com/questions/2005079/absolute-vs-relative-urls