How to get URL of current page in PHP

$_SERVER['REQUEST_URI']

For more details on what info is available in the $_SERVER array, see the PHP manual page for it.

If you also need the query string (the bit after the ? in a URL), that part is in this variable:

$_SERVER['QUERY_STRING']

If you want just the parts of URL after http://domain.example, try this:

<?php echo $_SERVER['REQUEST_URI']; ?>

If the current URL was http://domain.example/some-slug/some-id, echo will return only /some-slug/some-id.


If you want the full URL, try this:

<?php echo 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>

Tags:

Php