How URLs work in Drupal

By John Locke on February 22, 2010

URLs are a critical part of all web sites, not just Drupal. However, in Drupal, the URL of a page determines a lot about how the page is built.

A Uniform Resource Locater (URL) is simply the address of a particular page, what you see at the top of your browser starting with http:. In static web sites, a URL is simply a path to a file on a web server somewhere. In Drupal and in other content management systems and frameworks, a URL can be much more -- it can also contain a set of instructions for the program to run to generate something on the fly. Here's what you need to know about URL handling in Drupal.

Internal Paths

For most content items in Drupal, there are at least two different URLs to reach the item: the internal path, and one or more "friendly" or aliased URLs. The internal path to any item of content is node/<node id>. Add the domain name in front of this, and you've got a full URL. For example, this story has a node id of 407, so its internal path is node/407 and the full URL is http://freelock.com/node/407.

The Views module allows you to define arbitrary internal paths to any pages you create. About the only rule to paths provided by a view is that they take precedence over other paths that might be defined in the system--so the URL rules for views are very specific to an individual web site.

Other modules define other paths to generate particular pages. For example, the taxonomy module provides a path taxonomy/term/<term id>. If the site does not override this path with a view, you can see all content that's tagged with a particular taxonomy term with a link like http://freelock.com/taxonomy/term/13. Or you could if we hadn't rewritten that URL.

Most modules provide other internal paths to access settings pages and other pages they generate.

Friendly URLs, or Aliases

The Path module lets you create arbitrary paths to any internal path in the system, and combined with some clever rules in the Pathauto and Token modules, you can set up a site with an automatic URL scheme based on all kinds of different aspects of the content. These friendly URLs are also known as aliases.

One very common pattern we put in many sites is this: [vocab-raw]/[termpath-raw]/[title-raw]. What this does is it creates a URL structure based on how you tag an individual story. For example, you might have one taxonomy for Departments, and another taxonomy for Projects. These taxonomy names get converted to lower case and used as the first part of the URL, [vocab-raw]. The actual term you apply to a story becomes the second part, the [termpath-raw]. If you have a nested hieararchical taxonomy, this will add all the parent terms to the path for you. Finally, we use [title-raw] to convert the actual page title to something you can use in a URL. The end result is that instead of having a path like node/4493, we end up with department-owner/food-services/catering, a far more appetizing place to go. Search engines love friendly URLs like this, so they're often called "search-engine friendly" URLs.

You can use either an internal URL or an alias in block visibility settings. Aliases are particularly useful, however, since you can use a wild card to apply it to a lot of pages.

URLs for block visibility

So why is all of this important? Besides search engines, Drupal itself uses URLs for more than just which page of content to retrieve. The block system relies heavily on URLs, and can use either an internal path or a friendly URL to control whether a particular block appears on a page.

For example, if you want to create a sub-section of a site with a custom menu associated with a particular project, leveraging a clever URL scheme is one of the simplest ways of getting the job done. On the Bellingham School District site, for example, this is how the menu for the Culminating Project works. All pages that go into this section get tagged with the term "Culminating Project" from the "Projects" vocabulary. This gives each of these pages a URL that begins with projects/culminating-project/. And then in the block visibility settings for the Culminating Projects menu, we can simply show the menu only on pages that have the path projects/culminating-project/*.

Troubleshooting blocks and URLs

If you follow a link on a Drupal site and don't see the right combination of blocks, first take a look at the URL your browser is on. Then go to the block that's either appearing where it shouldn't, or not appearing when it should, and take a look at the block visibility settings to see if that path should be included or excluded. The fix might be as simple as correcting the block visibility settings.

It's also possible that another term took precedence, and so you've gotten a different URL for the page than you expect. With the automatic pattern we use, the friendly URL is generated from the first term and vocabulary it finds attached to the item, sorted by the order in the taxonomy module. So if a story has several terms applied, only one will be used to generate the path. If the wrong one is getting applied, you can drag the vocabularies and terms around to reorder them in the taxonomy manager.

Using Redirects

There's a very similar system for getting to content, called Path Redirect. Like the friendly URL system, it allows you to create custom URLs for particular internal paths. Unlike the alias system, you can have as many redirects as you want pointing to a given page.

An "alias" to an internal path essentially replaces the internal path. Under most circumstances, you'll only see the friendly URL when you visit a page, regardless of how you got there. A redirect is mainly a way to preserve old links. In general, search engine experts advise not having the exact same content on two different URLs--the search engines want to have a single URL for any given bit of content, and may penalize your site for having many different URLs with identical content. A redirect is the best way to fix a link that becomes broken. It's worth reviewing the Top page not found errors report on a regular basis, and if you find a bunch of traffic to a particular URL that's not found, you should create a redirect to the correct location.

Topic

Comments

Add new comment

The content of this field is kept private and will not be shown publicly.

Filtered HTML

  • Web page addresses and email addresses turn into links automatically.
  • Allowed HTML tags: <a href hreflang> <em> <strong> <blockquote cite> <cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h1> <h2 id> <h3 id> <h4 id> <h5 id> <p> <br> <img src alt height width>
  • Lines and paragraphs break automatically.

Drupal Canvas — Block HTML (locked)

  • Allowed HTML tags: <strong> <em> <u> <a href> <p> <br> <ul> <ol> <li>

Drupal Canvas — Inline HTML (locked)

  • Allowed HTML tags: <strong> <em> <u> <a href>

About the Author

Profile picture for user John Locke

John Locke is the lead developer and founder of Freelock, LLC. In addition to being a proficient web developer, he is an experienced technical writer, network administrator, and all around problem solver. He has worked with computers since 1984, and currently advises small businesses on open source software.

More Like This

Walking on path
🕑May 08, 2018 🖋Ben Weagraff 💬5

What is SEO?

SEO stands for Search Engine Optimization. According to Moz.com, SEO is “a marketing discipline focused on growing visibility in organic (non-paid) search engine results”.

SEO audit
🕑Apr 02, 2018 🖋Stuart Jenner 💬0

Paid Search or SEO: Which to Use?

A common question of companies wanting to drive traffic to their website is “Should we advertise in the search engines? Or conduct a search engine optimization project? Neither? Both?” Based on 20 years of helping people with marketing in search engines, here are a few thoughts.

Rock climb
🕑Aug 05, 2017 🖋John Locke 💬2

Getting the group into the URL with Purl

The corners of Drupal 8 that aren't there are quickly dwindling, but there are still some that need to get worked out. While upgrading our internal issue tracker, we hit a new one -- getting a group context set via a URL alias, and generally keeping posts within a group.

🕑Dec 19, 2013 🖋John Locke 💬1

The death of dot com? Get ready for a whole new web

December 2013

In a few short years, dot-com will be a quaint throwback referring to a couple of economic booms, and not something to indicate a web site. Like the Great War -- everybody now calls it World War 1, since we've had another great war... Why? Because the floodgates are about to open on domain names, and so we're about to see the rise of dot bike, dot plumbing, dot gifts, and even dot dentist.

🕑Aug 20, 2013 🖋John Locke 💬2

What is the value of your web site?

Results. Return On Investment. Value. How do you measure these things in a website? There's one thing you can easily measure -- cost. Or at least the amount you actually spend to build and maintain a site. The others are far more troublesome to measure.

🕑Apr 23, 2013 🖋John Locke 💬0

Put your blog on your main site!

I was talking with a new client the other day who spends a lot of money on Search Engine Optimization (SEO) and Search Engine Marketing (SEM) to try to get people to visit his online store. And yet his blog -- what search engines value most -- was on wordpress.com.