URL

The URL is the full address people type, share and bookmark; on a Drupal site its readable form comes from the page's alias, if one exists.

Learn DrupalDrupal glossary

A URL, short for Uniform Resource Locator, is the address of a page on the web. It names the protocol, the server and the path, and it can carry parameters and an anchor. In Drupal, the path part is matched to a route that builds the page, and the Url class generates URLs in code.

How URLs work in Drupal 11

MDN breaks a URL into the scheme (the protocol the browser uses), the authority (the domain and an optional port), the path, the parameters after a question mark and the anchor after a hash sign. In https://example.com/about?page=2#team the scheme is https, the domain is example.com, the path is /about, the parameter is page=2 and the anchor is team.

Drupal receives the request and matches the path to a route. If the page has an alias, AliasPathProcessor converts it to the system path first. In the other direction, the same class turns a system path into its alias whenever Drupal generates a link.

In code, Drupal\Core\Url holds the information about an address. Its factory methods include:

  • fromRoute() builds a URL for a routed Drupal page. The API documentation says code that links to Drupal pages should normally use it.
  • fromUri() takes a full URI with a scheme. It understands entity:, route:, internal: and base:, and treats other schemes such as https: as external.
  • fromUserInput() accepts only input that starts with /, ? or #.

toString() returns the address. Routed URLs go through the URL generator.

Example

use Drupal\Core\Url;

        $url = Url::fromUri('entity:node/7');
        $address = $url->setAbsolute()->toString();

The result is the full address of content item 7, with the domain. If that item has an alias, the generated address uses the alias, not /node/7.

Common mistakes

  • Passing a string without a scheme to fromUri(). It throws an InvalidArgumentException. Use fromUserInput() for a path that starts with a slash.
  • Using internal: for ordinary links. The API documentation limits it to processing user input.
  • Typing system paths by hand into code. A string such as /node/7 never passes through the URL generator, so Drupal cannot swap in the alias.
  • Changing a URL on a live page and doing nothing else. Add a redirect from the old address.

For marketing and content teams

The URL is what people read, share and bookmark, so it is part of the page's identity. Short, readable URLs come from aliases; the Pathauto module can create them from patterns. Plan the URL structure before launch, and treat every later change as a task that needs a redirect. Remember that anything after the # stays in the browser, so a link to a section of a page does not change what the server returns.

Sources

Questions about URLs in Drupal

MDN lists five: the scheme, such as https; the authority, which holds the domain and an optional port; the path; the parameters, a list of key and value pairs after a question mark; and the anchor, which starts with #.

Create a Url object and turn it into a string, for example Url::fromUri('entity:node/7'). The Drupal API documents that an entity: URI resolves to the canonical route of that entity.

Call setAbsolute() on the Url object before toString(). The API documentation names an RSS feed as an example of a link shown outside the site.

No. MDN says the part after the # is never sent to the server with the request. The browser uses it to show a place in the page.

Related terms

All Drupal terms from A to Z