How to Get URI in Joomla

The Uri Class is used to get information about the current Joomla URL and parts of the URL.

This class serves two purposes:

  1. It parses a URI and provides a common interface for the Joomla Platform to access and manipulate a URI.
  2. It obtains the URI of the current executing script from the server regardless of server.

To get the URL of the current webpage:

use Joomla\CMS\Uri\Uri;
$uri = Uri::getInstance();
$url = $uri->toString();

It returns the global Uri object, only creating it if it doesn't already exist.

Difference Between URL and URI

The URL stands for Uniform Resource Locator. The URI stands for Uniform Resource Identifier. A URI can be a name, locator, or both for an online resource whereas a URL is just the locator. URLs are a subset of URIs.

Components of URL or URI

A URL is normally made up of four components:

  1. Scheme: The scheme identifies the protocol used to access the resource on the Internet. It can be HTTP or HTTPS (with SSL).
  2. Host: The hostname identifies the host that holds the resource. For example,
  3. Path: The path identifies the specific resource in the host that the web client wants to access.
  4. Query: The query string follows the path. It provides information that the resource can use for several purpose. The query string is usually a string of name and value pairs separated by ampersand (&). For example, view=article&search=joomla.

The scheme is followed by a colon and two forward slashes. If a port number is specified, that number follows the host name, separated by a colon. The path name begins with a single forward slash. If a query string is specified, it is preceded by a question mark.

A URL can be followed by a fragment identifier. The separator used between the URL and the fragment identifier is the # character. A fragment identifier is used to point a web browser to a reference or function in the item that it has just retrieved. For example, if the URL identifies an HTML page, a fragment identifier can be used to indicate a subsection within the page, using the ID of the subsection.

Joomla Uri Class

Namespace: use Joomla\CMS\Uri\Uri;

1. To get the global Uri object, only it if it doesn't already exists.

$uri = Uri::getInstance();

2. To get the complete or full URI

echo $uri;


echo $uri->toString();

3. To get the scheme (http or https)

echo $uri->getScheme();

To check if the scheme uses SSL

echo $uri->isSsl();

4. To get the host

echo $uri->getHost();

It returns the hostname or ip or null if no hostname or ip was specified.

5. To get the fragment (everything after the '#'):

echo $uri->getFragment();

6. To get the base URL or root URL

echo $uri->base();
echo $uri->root();
echo $uri->root(true); // returns path only

7. To get the path of URL after the root.

echo $uri->getPath();

It includes a forward slash at the starting and excludes any query string parameters.

8. To get the URL without any query string parameters

echo $uri->current();

9. To get the query string parameters

echo $uri->getQuery();

You can add the true argument to get query string parameters as array.

$arr = $uri->getQuery(true);

To get a specific query string variable by name 

echo $uri->getVar('type');

You can also declare a default value as the second argument if the variable is not set.

To check if a variable exists

echo $uri->hasVar('type');

To Remove an item from the query string variables if it exists


10. To get URI port number


It returns null if no port was specified.

11. The toString() converts the Uri to a string, and allows you to select the parts of the URL which you want.

echo $uri->toString(array('scheme','host','port','path');

External URLs

The isInternal() returns true if the URL is within the Joomla instance, false otherwise. This is a static function, so you have to pass the URL as a string.

$internal = Uri::isInternal($url);

Usually, just specify the URL as a string. However, you can use Uri class to manipulate parts of a URL.

$joomla = Uri::getInstance("//");
$joomlaNews = $joomla->toString();

Internal Static URLs

To output a URL link to a file within the Joomla, use:

$url = Uri::root() . 'path/from/joomla/root/to/file.typ';

For example,

$url = Uri::root() . 'images/picture.jpg';

The advantage of this approach is that no changes are required if you change the name of your site or domain.

Internal Dynamic URLs

To create a URL which points to an item which is managed by a Joomla component:

use Joomla\CMS\Router\Route;
$url = Route::_("index.php?option=com_example&view=showitem&id=14");

Joomla Standard Query Parameters

option: The component (example: com_contact) associated with the webpage.

view: The view which the component displays on that page (example: article for com_content).

layout: The layout php file (in tmpl directory under the view) to be used.

id: The id of the item to be shown.

catid: The id of the category associated with the item.

Itemid: The id of the menu item which points to the webpage.

format: The output format (html, json, jsonapi, feed, xml, raw, image) expected. The output format by default is html.