function createAbsoluteUrl($url, $base) { //check input if (empty($url) || empty($base)) { return NULL; } // php parse_url will go berserk with space in front // just try parse_url on " http://www.3mik.com/item/1" $url = trim($url); $base = trim($base); $scheme = \parse_url($url, PHP_URL_SCHEME); if (!empty($scheme)) { return $url; } // Urls only containing query or anchor if (Util::startsWith($url, "#") || Util::startsWith($url, "?")) { return $base . $url; } // Parse base URL and convert to local variables: $scheme, $host, $path $pieces = \parse_url($base); $scheme = isset($pieces["scheme"]) ? $pieces["scheme"] : ""; $host = isset($pieces["host"]) ? $pieces["host"] : ""; // If no path, use / $path = isset($pieces["path"]) ? $pieces["path"] : "/"; // Remove non-directory element from path $path = preg_replace('#/[^/]*$#', '', $path); // Destroy path if relative url points to root if (Util::startsWith($url, '/')) { $path = ''; } // Dirty absolute URL $abs = "{$host}{$path}/{$url}"; // Replace '//' or '/./' or '/foo/../' with '/' $re = array('#(/\\.?/)#', '#/(?!\\.\\.)[^/]+/\\.\\./#'); for ($n = 1; $n > 0; $abs = preg_replace($re, '/', $abs, -1, $n)) { } // Absolute URL is ready! return $scheme . '://' . $abs; }