Skip to content

Instantly share code, notes, and snippets.

@andrewspear
Last active December 19, 2015 19:18
Show Gist options
  • Save andrewspear/6004632 to your computer and use it in GitHub Desktop.
Save andrewspear/6004632 to your computer and use it in GitHub Desktop.
A simple PHP function to fetch the current page URL with PHP, .htaccess rewrites are respected and optionally you can exclude the query string, useful for creating canonical URLs.
<?
function currentPageURL($includeQueryString = true) {
$pageURL = 'http';
$pageURL .= ($_SERVER["HTTPS"] == "on") ? "s" : "";
$pageURL .= "://";
$pageURL .= ($_SERVER["SERVER_PORT"] != "80") ? $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"] : $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
if (!$includeQueryString) {
$pageURL = str_replace('?'.$_SERVER["QUERY_STRING"], '', $pageURL);
}
return $pageURL;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment