Skip to main content

The PHP function will get the current requested URL. It also takes into account default HTTP ports, and format them accordingly.

/**
 * Get the current Url taking into account Https and Port.
 *
 * @link http://css-tricks.com/snippets/php/get-current-page-url/
 *
 * @version Refactored by @AlexParraSilva
 */
function getCurrentUrl()
{
    $url = isset($_SERVER['HTTPS']) && 'on' === $_SERVER['HTTPS'] ? 'https' : 'http';
    $url .= '://'.$_SERVER['SERVER_NAME'];
    $url .= in_array($_SERVER['SERVER_PORT'], array('80', '443')) ? '' : ':'.$_SERVER['SERVER_PORT'];
    $url .= $_SERVER['REQUEST_URI'];

    return $url;
}