{"id":109,"date":"2019-09-16T16:30:20","date_gmt":"2019-09-16T16:30:20","guid":{"rendered":"https:\/\/tutorialio.com\/?p=109"},"modified":"2025-11-04T10:31:59","modified_gmt":"2025-11-04T10:31:59","slug":"get-the-full-url-of-current-page-in-php","status":"publish","type":"post","link":"https:\/\/hellonitish.com\/blog\/get-the-full-url-of-current-page-in-php\/","title":{"rendered":"How Can I Get the Full URL of Current Page in PHP?"},"content":{"rendered":"<p>There are a lot of situations where you might need to get the URL of a webpage that any user might be currently visiting. For instance, a project may require you to dynamically generate the title of a webpage based on its URL. There are many ways in which we can obtain the URL of a webpage and we will discuss them all in detail in this tutorial.<\/p>\n<h2 id=\"get-the-full-url-with-query-string-using-serverrequesturi\">Get the Full URL with Query String Using $_SERVER[&#8216;REQUEST_URI&#8217;]<\/h2>\n<p>The <a href=\"http:\/\/php.net\/manual\/en\/reserved.variables.server.php\">$_SERVER superglobal array<\/a> in PHP contains a lot of useful information like the headers, timestamp for the request, paths and script locations. For this particular tutorial, we will be looking into several indices of the <code>$_SERVER<\/code> array that can be used to construct the full URL of the current page with the parameters.<\/p>\n<p>You can use the value of <code>$_SERVER['HTTP_HOST']<\/code> to get the contents of the <strong>Host:<\/strong> header for current request. In other words, <code>$_SERVER['HTTP_HOST']<\/code> returns the domain name of the current page. <\/p>\n<p>Similarly, you can use <code>$_SERVER['REQUEST_URI']<\/code> to get the URI that was given in order to access the current page. This value will include the query string of the URL as well.<\/p>\n<p>The only thing that we need to know now is if the URL was served over HTTP or HTTPS. This can be done by checking the value of <code>$_SERVER['HTTPS']<\/code>. When a page is served over HTTPS, this element is set to an non-empty value. However, there is a catch. When using ISAPI with IIS, the value of this element will be set to off. What we need to do in this situation is check if a value has been set and verify that it is not equal to <code>off<\/code>. <\/p>\n<p>The example below provides a simple solution to get the full URL of current page the user is visiting with the query string intact.<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">$protocol = isset($_SERVER['HTTPS']) &amp;&amp; $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http';\n$full_url = $protocol.\":\/\/$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]\";\necho $full_url;\n<\/code><\/pre>\n<p>I have tested the above code on a test page that I created and it produced the following output.<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">URL in address bar \u2014 https:\/\/tutorialio.com\/php\/faqs\/index.php\nURL output by above code \u2014 https:\/\/tutorialio.com\/php\/faqs\/index.php\n\nURL in address bar \u2014 https:\/\/tutorialio.com\/php\/faqs\/index.php?page=12&amp;sort=views\nURL output by above code \u2014 https:\/\/tutorialio.com\/php\/faqs\/index.php?page=12&amp;sort=views\n\nURL in address bar \u2014 https:\/\/tutorialio.com\/php\/faqs\/index.php?page=12&amp;sort=views#something\nURL output by above code \u2014 https:\/\/tutorialio.com\/php\/faqs\/index.php?page=12&amp;sort=views<\/code><\/pre>\n<p>As you can see, the URL contains all the parameters of the query string. The only problem is that we cannot get the value of any fragment identifier present in the URL. This is because the fragment identifier is not passed to the server at all. If you need access to the fragment identifier you will have to do it client-side using JavaScript.<\/p>\n<h2 id=\"get-the-full-url-without-query-string-using-serverscripturi\">Get the Full URL Without Query String Using  $_SERVER[\u2018SCRIPT_URI\u2019]<\/h2>\n<p>If you have <a href=\"http:\/\/httpd.apache.org\/docs\/2.2\/mod\/mod_rewrite.html#EnvVar\">enabled mod_rewrite<\/a>, you can also use <code>$_SERVER['SCRIPT_URI']<\/code> to get the whole URL at once. The only drawback of this method is that you will get the URL without the query string. However, if you were planning to discard the query string anyway, this is an elegant one line solution.<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">$full_url = ['SCRIPT_URI'];\necho $full_url;\n\n\/* URL in address bar \u2014 https:\/\/tutorialio.com\/php\/faqs\/index.php\nURL output by above code \u2014 https:\/\/tutorialio.com\/php\/faqs\/index.php\n\nURL in address bar \u2014 https:\/\/tutorialio.com\/php\/faqs\/index.php?page=12&amp;sort=views\nURL output by above code \u2014 https:\/\/tutorialio.com\/php\/faqs\/index.php\n\nURL in address bar \u2014 https:\/\/tutorialio.com\/php\/faqs\/index.php?page=12&amp;sort=views#something\nURL output by above code \u2014 https:\/\/tutorialio.com\/php\/faqs\/index.php *\/\n<\/code><\/pre>\n<h2 id=\"remove-query-string-from-url-using-strreplace\">Remove Query String From the URL Using str_replace()<\/h2>\n<p>We already have the whole URL of the current page thanks to the code we wrote in first section. Even though we got the URL without the query string in the second section, that method only works if mod_rewrite has been enabled. If you can\u2019t make any assumptions about mod_rewrite, you should get the whole URL and then remove the query string using inbuilt PHP functions.<\/p>\n<p>You can easily obtain the query string of a URL, with the help of <code>$_SERVER['QUERY_STRING']<\/code>. Once you have the query string, you can use the <a href=\"..\/string\/str-replace-function.php\">string_replace() function in PHP<\/a> to replace it with an empty string. The following example should make it clear.<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">\/\/ Test URL \u2014 https:\/\/tutorialio.com\/php\/faqs\/index.php?page=12&amp;sort=views\n\n$protocol = isset($_SERVER['HTTPS']) &amp;&amp; $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http';\n\n\/\/ $full_url = https:\/\/tutorialio.com\/php\/faqs\/index.php?page=12&amp;sort=views\n$full_url = $protocol.\":\/\/$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]\";\n\n\/\/ $query_string = page=12&amp;sort=views\n$query_string = $_SERVER['QUERY_STRING'];\n$params = '?'.$query_string;\n\n\n\/\/ $no_params_url = https:\/\/tutorialio.com\/php\/faqs\/index.php\n$no_params_url = str_replace($params, '', $_SERVER[REQUEST_URI]);\n<\/code><\/pre>\n<h2 id=\"remove-query-string-from-the-url-using-explode\">Remove Query String From the URL using explode()<\/h2>\n<p>Another PHP function that you can use to get rid of the query string in a URL is <a href=\"..\/string\/explode-function.php\">explode()<\/a>. The <code>explode()<\/code> function basically splits a string into an array of smaller substrings using given delimiter.<\/p>\n<p>To remove the query string in our URL, we can use the full URL as the main string and a \u2018?\u2019 as the delimiter. The following code snippet will make things more clear:<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">\/\/ Test URL \u2014 https:\/\/tutorialio.com\/php\/faqs\/index.php?page=12&amp;sort=views\n\n$protocol = isset($_SERVER['HTTPS']) &amp;&amp; $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http';\n\n\/\/ $full_url = https:\/\/tutorialio.com\/php\/faqs\/index.php?page=12&amp;sort=views\n$full_url = $protocol.\":\/\/$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]\";\n\n\/\/ $no_params_url = https:\/\/tutorialio.com\/php\/faqs\/index.php\n$no_params_url = explode('?', $full_url, 2)[0];\n<\/code><\/pre>\n<p>A lot is going on in the last line of our code, the <code>explode()<\/code> function splits <code>$full_url<\/code> into smaller components which stored as array elements. The URL without the query string will be stored in the first element of the resulting array. We just access it and assign the value to <code>$no_params_url<\/code>.<\/p>\n<h2 id=\"remove-parameters-from-the-url-using-strtok\">Remove Parameters From the URL Using strtok()<\/h2>\n<p>Some people might not prefer the <code>explode()<\/code> function to remove the parameters in a URL because it returns an array and they have to access the first element of that array in order to get the URL.<\/p>\n<p>You can also remove any parameters from the full URL string using the <a href=\"..\/string\/strtok-function.php\">strtok() function in PHP<\/a>. This function also splits a string into smaller tokens but it returns a string instead of an array.<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">\/\/ Test URL \u2014 https:\/\/tutorialio.com\/php\/faqs\/index.php?page=12&amp;sort=views\n\n$protocol = isset($_SERVER['HTTPS']) &amp;&amp; $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http';\n\n\/\/ $full_url = https:\/\/tutorialio.com\/php\/faqs\/index.php?page=12&amp;sort=views\n$full_url = $protocol.\":\/\/$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]\";\n\n\/\/ $no_params_url = https:\/\/tutorialio.com\/php\/faqs\/index.php\n$no_params_url = strtok($full_url, '?');\n<\/code><\/pre>\n<h2 id=\"quick-summary\">Quick Summary<\/h2>\n<p>Let\u2019s recap everything that we have covered in this tutorial.<\/p>\n<ol id=\"summary-points\">\n<li>You can get the full URL of current page along with the query string, using a combination of <code>$_SERVER[\"HTTP_HOST\"]<\/code> and <code>$_SERVER[\"REQUEST_URI\"]<\/code>.<\/li>\n<li>If you want the URL without the query string, the easiest way to get it will be with <code>$_SERVER[\"SCRIPT_URI\"]<\/code>. However, this will only work if <code>mod_rewrite<\/code> has been enabled.<\/li>\n<li>If you can\u2019t rely on <code>mod_rewrite<\/code>, the best way to get the URL of current page without the query string would be to first get the full URL. Once you have the full URL, you can use functions like <code>str_replace()<\/code>, <code>strtok()<\/code> and <code>explode()<\/code> to remove the query string.<\/li>\n<\/ol>\n<p>Let me know if there is anything that you would like me to clarify in this tutorial. Also, you are more than welcome to comment if you know other techniques to get the full URL of current page with or without the query string in PHP.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are a lot of situations where you might need to get the URL of a webpage that any user might be currently visiting. For instance, a project may require you to dynamically generate the title of a webpage based on its URL. There are many ways in which we can obtain the URL of [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-109","post","type-post","status-publish","format-standard","hentry","category-how-to-guides"],"_links":{"self":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/109","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/comments?post=109"}],"version-history":[{"count":1,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/109\/revisions"}],"predecessor-version":[{"id":10244,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/109\/revisions\/10244"}],"wp:attachment":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/media?parent=109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/categories?post=109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/tags?post=109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}