So I am attempting to parse out the URL, so that I can get a specific page template to be used for a specific URL; if the URL is example.com/team, then use page-team.tpl.php.

However, when I do this, the URL I am getting (via $_GET['q']) always seems to be the URL from where I was, not the one where I am currently at. If I directly navigate to example.com/team/surf, it gives me "team/surf"; if I go to example.com/team it gives
"team/surf." If I refresh the page, it will then give me the desired one.

I dropped this code into my template.php file.

function theme_preprocess(&$vars, $hook) {
  switch ($hook) {
    case 'page':   
      $normal_path = trim($_GET['q'], '/');
      $path_alias = drupal_get_path_alias($normal_path);
      $alias_parts = explode('/', $path_alias);

      if (($alias_parts[0] === 'team') && (count($alias_parts)) == 1) {
       $vars['template_file'] = 'page-team';

Do you have any idea on what is going on here?

  • 1
    Consider posting your solution as an answer and marking it as accepted so that others can easily find a solution
    – Laxman13
    Feb 11, 2012 at 17:21
  • yup. didn't realize that. i'll have to update the answer in 4hrs.
    – au_stan
    Feb 11, 2012 at 17:59
  • It seems a localized situation; $_GET['q'] is normally updated with the current value get from the page request. I have never seen that value being not correct. There could be code that alters that value, but from what being reported is not possible to know that.
    – apaderno
    Feb 11, 2012 at 21:09

2 Answers 2


I'm not sure why your code happens to fail in some cases, and in some cases not. But what I would try is replacing that code with the provided by Drupal arg function.

On "team/surf", arg(0) will return "team", and arg(1) "surf". This is what I use myself, and hopefully that will solve the problem for you.

  • interestingly it came down to dsm(). So i wanted to check some variables before i moved forward. i was simply dumping them into dsm() just to see what my results were looking like. at the time, just seemed like a good idea. so dsm($_GET['q']) would output the previous url and not the current url. when i did a print $_GET['q'] it would show me the current url as expected. as for arg(). good call on that one. i have seriously forgotten all about that one. thanks for the help.
    – au_stan
    Feb 11, 2012 at 22:16
  • @austin That is what happens sometimes with dsm().
    – apaderno
    Feb 12, 2012 at 1:53
  • 1
    THEME_preprocess_page() is called after the messages HTML has been produced. So, when drupal_set_message() or anything that ends calling it is called from THEME_preprocess_page(), the message will only be displayed on the next page request. It explain why dsm($_GET['q']) prints the previous URL. Also, note the dpm() should be used instead of dsm(). Feb 13, 2012 at 0:24
  • @Letharion - Pure arg() doesn't work with aliases. You have to add drupal_get_path_alias() as an argument of arg() like so: arg(1, drupal_get_path_alias()). Otherwise arg(1) will always return the node id.
    – leymannx
    Apr 9, 2014 at 11:22

There is no need to parse the $_GET['q'] yourself.

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.