I have a block I'm displaying using block setting's "Only the listed pages". I'm using a wild card asterisk to show the block on any pages following about-us/. For example: about-us/*

The problem is I ONLY want pages immediately after about-us/ to show. For example about-us/faq would show the block but about-us/faq/content should NOT show the block.

I tried about-us/*/ but that had no effect.

Any suggestions?

7 Answers 7


Method 1. PHP filter

I think your question is partially answered here:

How to add page restrictions for blocks in drupal?

Only you might use this code:

$arg = explode('/', drupal_get_path_alias());
if ($arg[0] == 'about-us' && $arg[1] && $arg[2] == NULL) {
  return TRUE;
else {
  return FALSE;

Method 2: Context module

Alternatively, you can try the Context module, which offers more robust visibility options. For instance, in addition to including paths you can prefix a path with a tilde "~" to exclude it.

I think this should do it:


Edit: Added second method.

Edit: Using $_GET['q'] in method 1 instead of arg() (Thx, @leymannx!)

  • Still getting false/no-display from this code
    – Sam
    Aug 15, 2015 at 17:17
  • What is the path where you are getting a FALSE return? Aug 15, 2015 at 17:19
  • I've added a second method. Aug 15, 2015 at 17:19
  • 1
    You have to check the path alias, which arg() doesn't.
    – leymannx
    Aug 16, 2015 at 6:52
  • 2
    You don't need to pass $_GET['q'] to drupal_get_path_alias() as "If no path is provided, the function will return the alias of the current page." Aug 17, 2015 at 13:56

Drupal 8 - Simple module to achieve this: https://www.drupal.org/project/block_exclude_pages

Simply add the path you want to exclude with a '!' Prefixed.




Set your block display settings to show on about-us/* then add the following hook to your module.

 * Implements hook_block_list_alter().
function my_module_block_list_alter(&$blocks) {
  $alias = explode('/', drupal_get_path_alias());
  if ($alias[0] == 'about-us' && isset($alias[1]) && isset($alias[2])) {
    // unset the bid (block id)

Edit: fixed condition - arg() doesn't return alias.


In Drupal 8, you can use the Block Visibility Groups module to very easily combine multiple conditions for one or many blocks. For this case, you can add a page [path]/* condition and a negative [path]/*/* condition.


Using PHP filter module In Block settings > Pages: check

Pages on which this PHP code returns TRUE (experts only)

Paste this:

return preg_match('/^about-us(\/[a-z]*)?(\/)?$/',
  • Oh, no, It's too late)) Aug 15, 2015 at 18:33
  • preg_match() seemed like a good way to go but unfortunately with this code I was getting int(0) result on both about-us/test-1 and about-us/test-1/test-2. Aug 16, 2015 at 7:32
  • Its because after first slash regexp searchs for letters only, did not u see expression? Aug 16, 2015 at 7:34
  • Well, now I do. Numbers do show up in aliases time and again so best to cover all bases, no? Aug 16, 2015 at 7:42
  • I just gave variant. And it's simple to find how to search for any symbol Aug 16, 2015 at 7:55

There is another alternative to answer this question ... which doesn't require the PHP filter to be enabled (which you should try to avoid whenever possible) ... Just use the Rules block visibility module. Here is a quote from its project page:

The Rules block visibility module allows Rules components to be used to control block visibility. This provides Drupal administrators and developers extreme flexibility in controlling when blocks should be displayed on their websites, in addition to the default visibility options provided by Drupal.

The general idea is that if you can do it with Rules, you can use it to control block visibility, so the possibilities are limitless.

Need to show a block only for users registered more than a month ago?

Perhaps you have a block that must be shown only between 8am-5pm on weekdays?

What about displaying or hiding a block based on current weather conditions?

All of this can be done by using Rules block visibility.

With that, and as per the "if you can do it with Rules, you can use it to control block visibility" above, you've reduced your question to making Rules "check the path of the current page" (so that the block is only shown on your pages with a path that starts with "about-us/", but not on those pages that have a path like "about-us/*/*").

For an illustration of how to use this module, refer to my answer to "How to stop a Views block from displaying to admins, such as user/1?". It includes a Rules Component (in Rules export format), which you should replace by this variation of it to make it match your specific question):

{ "rules_block_visibility_show_on_selected_profile_pages" : {
    "LABEL" : "Show block on selected profile pages",
    "PLUGIN" : "rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
      "module" : { "label" : "Module", "type" : "text" },
      "delta" : { "label" : "Delta", "type" : "text" },
      "result" : { "label" : "Result", "type" : "boolean", "parameter" : false }
    "IF" : [
      { "AND" : [
          { "text_matches" : {
              "text" : [ "site:current-page:path" ],
              "match" : "about-us\/",
              "operation" : "starts"
          { "NOT text_matches" : {
              "text" : [ "site:current-page:path" ],
              "match" : "about-us(\/[a-z]*)?(\/)?$/",
              "operation" : "regex"
    "DO" : [ { "data_set" : { "data" : [ "result" ], "value" : "1" } } ],
    "PROVIDES VARIABLES" : [ "result" ]

Note: the 2nd Rules Condition in the above Rules Component (with the regular expression in it) may need further refinements to perfectly fit your case. I.e. the "about-us(\/[a-z]*)?(\/)?$/" part of it (though I think it's pretty close to what your question is about).


The go to Drupal 8/9/10 solution for both including and excluding pages at the same time is the Condition Path module.

It uses the condition plugin API which makes it useful for all content implementing conditions (such as blocks).

Example: To show a block on all product pages but not on the subpage 'reviews', add the following pages in the condition:

  • /products/*
  • !/products/*/reviews

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.