{"id":2529,"date":"2019-12-30T14:17:09","date_gmt":"2019-12-30T14:17:09","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=2529"},"modified":"2025-12-03T21:36:31","modified_gmt":"2025-12-03T21:36:31","slug":"get-page-uri-url","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/php\/page-control\/get-page-uri-url","title":{"rendered":"Get Page URI \/ URL"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Get just the domain name<\/h4>\n\n\n\n<p>$_SERVER[&#8216;HTTP_HOST&#8217;] gives you the domain name through which the current request is being fulfilled<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  $OurHttpHost = $_SERVER&#91;'HTTP_HOST'];   \/\/Returns just the domain name\n  \/\/for \"https:\/\/mydomain.com\/somepage\" $OurHttpHost will be \"mydomain.com\"\n\n  \/\/If you need to ensure www. is not part of it (will be included if part of the request)\n  $OurHttpHost = str_replace(\"www.\", \"\", $OurHttpHost);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Get site URL<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>$SiteUrl = \"https:\/\/\" . $_SERVER&#91;'HTTP_HOST']\nor ensuring there is no www:\n$SiteUrl = \"https:\/\/\" . str_replace(\"www.\", \"\", $_SERVER&#91;'HTTP_HOST']);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Get full page URL (domain name + page + any arguments)<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>$ThisPageUrl = \"https:\/\/\" . $_SERVER&#91;'HTTP_HOST'] . $_SERVER&#91;'REQUEST_URI'];<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Page URL (With Arguments)<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>$ThisPageUrl = $_SERVER&#91;\"REQUEST_URI\"];<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Stripping Page URL<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">Get URL without the domain name<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>  $UrlPath = $_SERVER&#91;'REQUEST_URI'] ?? '\/';\n  \/\/For \"https:\/\/example.com\/my\/page?x=1&amp;y=2\"\t$UrlPath will be \"\/my\/page?x=1&amp;y=2\"\n  \/\/For \"http:\/\/example.com\/\t$UrlPath will be \"\/\"\n  \/\/For \"https:\/\/example.com\/path\/only\t$UrlPath will be \"\/path\/only\"<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Getting URL without the arguments<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>$UrlWithoutArguments = parse_url($_SERVER&#91;\"REQUEST_URI\"], PHP_URL_PATH);    \/\/Get page without any url arguments\n \n\/\/for \"https:\/\/mydomain.com\/somedirectory\/somepage.php?auth=1234\" $UrlWithoutArguments will be \"\/somedirectory\/somepage.php\"<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Getting just the URL arguments<\/h5>\n\n\n\n<p>Will get everything after the &#8216;?&#8217; (excluding the &#8216;?&#8217;)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$UrlArguments = parse_url($_SERVER&#91;\"REQUEST_URI\"], PHP_URL_QUERY);<\/code><\/pre>\n\n\n\n<p>Example usage:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  $UrlArguments = parse_url($_SERVER&#91;\"REQUEST_URI\"], PHP_URL_QUERY);\n\n  $RedirectUrl = \"https:\/\/mydomain.com\";\n  if (strlen($UrlArguments) &gt; 0)\n    $RedirectUrl .= '?' . $UrlArguments;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Does URI match?<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">Stripping any url arguments from it first<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>$ThisPageUrl = parse_url($_SERVER&#91;\"REQUEST_URI\"], PHP_URL_PATH);      \/\/Get page without any url arguments\nif($ThisPageUrl == '\/my_page_name')<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">With any url arguments<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>if($_SERVER&#91;\"REQUEST_URI\"] == '\/my_page_name?i=1')<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Does URI contain?<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>if (strpos($_SERVER&#91;\"REQUEST_URI\"], 'admin') === False)\nor\nif (strpos($_SERVER&#91;\"REQUEST_URI\"], 'admin') !== False)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Get just the domain name $_SERVER[&#8216;HTTP_HOST&#8217;] gives you the domain name through which the current request is being fulfilled Get site URL Get full page URL (domain name + page + any arguments) Page URL (With Arguments) Stripping Page URL Get URL without the domain name Getting URL without the arguments Getting just the URL [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,56,386],"tags":[],"class_list":["post-2529","post","type-post","status-publish","format-standard","hentry","category-page-control","category-urls","category-urls-wordpress"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2529","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=2529"}],"version-history":[{"count":24,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2529\/revisions"}],"predecessor-version":[{"id":5233,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2529\/revisions\/5233"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=2529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=2529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=2529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}