drupal links how-to

Creating links in Drupal

Disclosure: Just so you know, we may sometimes earn a small commission from affiliate links on this site.

Creating internal hyperlinks in Drupal, from one page to another, can be slightly tricky - node IDs, path aliases, the Drupal url() function, all play a role.

Fully qualified links, those that contain the entire address including http, are easy enough to use, but it's not good practice to enter a static, fully qualified URL when linking from one page or blog post to another.

Fully qualified links, by definition, include the domain name. If that changes all these links break, and, if the site is substantial, it would require a lot of work to fix everything.

Luckily there is a better way...

Creating Drupal hyperlinks the wrong way

Trying to implement href attributes within Drupal links like this:

<a href="content/creating-links-drupal">creating links in Drupal</a>

will end up with in heartbreak because the relative path is simply added to the current path.

For example, if the above link was displayed on this site's homepage, it would correctly link to its page. If it showed from somewhere else (like in this article) it would break, because it would try to link to content/content/creating-links-drupal (try the static link here).

Note that the relative path has simply been tacked on without regard for whether the page exists.

Add a leading slash to correct the hyperlink

You can add a leading slash to the relative URL (i.e. /content/creating-links-drupal) to force it to be added to the base path. This is a much better solution, and the one that most people use, but it can still lead to problems with changing path aliases, if you're not careful.

Creating Drupal links with the url() function

Assuming that you are the site administrator/owner, or at the very least, someone with sufficient permissions to use the PHP input format, then there is a Drupal function available to help correctly create links:

The url() function takes a path and generates a URL string from it.

Our standard link notation now becomes:

<a href="<?php print url('relative/path/to/content'); ?>">link text</a>

We have printed the results of the url() function into the href attribute of the anchor tag. Creating links like this ensures that no matter where the link is displayed in a site (it could be from a view, from the front page, from another blog post), it will always be correct.

But it still relies on the path alias...

Drupal hyperlinks with node ID and url()

If you are using the Pathauto module to generate aliases for SEO purposes, then you should always link to the node ID of the target content because the node ID can never change once the node is created.

The path alias can change easily. For example, if you decide to alter the title, the alias will automatically change to fit the new title and any links to the old alias will break.

In this case, the above link should be <a href=" <?php echo url('node/238'); ?>">link text</a>.

It's easy to find the node ID of any content by scanning the page source for that page, or getting the node ID from the edit page URL of that node - assuming you have edit permissions.

Of course, allowing PHP code to be executed within content is not very security conscious, so there must be a better way to go about this.

Drupal hyperlinks with node ID but without PHP

If you don't want to use the PHP text format (which is sensible), then try using the node ID with the leading slash, like this <a href="/node/238">link text</a> (try the node ID link here). Note that Drupal correctly redirects to the assigned URL alias.

Struggling with Drupal?

If you're struggling with Drupal then there are a few steps you can take to try and resolve your problem as quickly as possible:

  1. Search on Google
  2. Search on drupal.org
  3. Ask a question in the Drupal forums

If you are still having trouble sorting out Drupal hyperlinks, drop a comment below and I'll try help out.

Back to Top