I could not figure out how to create Url Alias to change exposed filter url from e.g. www.example.com/viewabc?tid=11 to www.example.com/viewabc/xyz where xyz is 11th taxonomy term.

How can I get rid of "?".

Sorry if this is too trivial question, I couldn't find a solution.

CommentFileSizeAuthor
#7 path.admin_.inc_.diff545 bytesgravisrs

Comments

merlinofchaos’s picture

Status: Active » Closed (works as designed)

URL aliases don't support query arguments; at the moment there is no way to do this. Someone expert might be able to figure out a way to do it with argument defaults and stuff, but for now exposed filters simply can't do what you want.

sbydrupal’s picture

Thanks Merlin for quick reply.

Is this a solution for exposed filter Alias from previous posting "http://drupal.org/node/118072" with title "
"?" not supported in a path alias?"

================================================
imrook - March 23, 2008 - 00:28

I have recently run into this problem and have a solution that meets my needs so I'll chime in with my two cents here. The part after the '?' character in the URL is called the query string. boombatower created an issue with a patch for this problem here. Some users complained that it didn't work -- I personally haven't tested it. chx doesn't seem too intent on adding the functionality at this time.

But there is still hope. Here's why the problem occurs in the first place: Apache uses mod rewrite to rewrite a URL like http://mysite.com/drupal/admin/build/menu to become http://mysite.com/drupal/index.php?q=admin/build/menu. During the bootstrap phase, Drupal uses the value of the variable $_GET['q'] to map a URL to a function call. Now look what happens with the following URL: http://mysite.com/drupal/path_alias?myvar=1 Apache's mod_rewrite turns it into http://mysite.com/drupal/index.php?q=path_alias&myvar=1 This is normally a good thing. The problem with Drupal is that the core doesn't look at any other variables besides $_GET['q'] so it will only use path_alias for the lookup. Luckily Drupal provides a place where we can change that: custom_url_rewrite() Add the following code to your settings.php file:

function custom_url_rewrite($op, $result, $path) {
  global $user;

  if ($op == 'source') {
    // Get extra variables from URL
    if (isset($_SERVER['QUERY_STRING'])) {
      //Remove the 'q=' and change the first & into a ?
      $query = preg_replace('/^q=([^&]*)&(.*)$/', '\1?\2', $_SERVER['QUERY_STRING']);
    }
    // See if we have a url_alias for our new path
    $sql = 'SELECT src FROM {url_alias} WHERE dst="%s"';
    $row = db_result(db_query($sql, $query));
    if ($row) {
      // We found a node source path
      $result = $row;
    }
  }

  return $result;

}

This will allow you to set a path alias for a node that contains a query string and Drupal will redirect to the correct page. Note that when Drupal generates a link to the node it will be urlencoded, but it will still work properly.

This code is actually written for 5.x In Drupal 6, the function is called custom_url_rewrite_inbound and the parameters have changed a bit. I haven't taken a look at 7 yet so I don't know anything about that.
==================================

j0k3z’s picture

Did anyone figure out the Drupal 6 code?

heyyo’s picture

Any update on this issue ?

heyyo’s picture

Status: Closed (works as designed) » Active
dawehner’s picture

Status: Active » Closed (works as designed)

See above #2

gravisrs’s picture

StatusFileSize
new545 bytes

For those who couldn't find how to make it in Drupal 6:


function custom_url_rewrite_inbound(&$result, $path) {
  global $user;

  //See if we have source for given path alias
  $sql = 'SELECT src FROM {url_alias} WHERE dst="%s"';
  $row = db_result(db_query($sql, $path));
  if ($row) {
    //separate query string
    $split = explode('?',$row);
    //if query string exists
    if (isset($split[1])) {
      //populate $_GET params (for views exposed filters etc.)
      $additional_params = explode('&',$split[1]);
      foreach ($additional_params as $parm){
        $parmarr = explode('=',$parm);
        $_GET[$parmarr[0]] = $parmarr[1];
      }
    }
    //result should be q= part only - must be found in menu router
    $result = $split[0];
  }
}

Sorry for bit dirty code. There should be few more checks/ifs to make it bulletproof but the main idea is included.

Now, to make it possible to add aliases with additional query parameters in admin/build/path/add we could use attached core hack to bypass form validation. [Or write a module to hook validation and bypass it in 'proper' way].

alladdin’s picture

The code in #7 is not working on drupal 6.2 with views 3.

admin/build/path is not accessible with 404 error.

Can someone please finalize this code.

marko b’s picture

I am also very interested in this. Why is this any different (views) than normal url rewrite? they are just parametars sent with get method, right? I must be missing something in complexity...

brian_c’s picture

I have this working for D7 (and Views 3) via hook_url_inbound_alter(): http://drupal.org/node/118072#comment-4706374

killah89’s picture

Im very interested too in this code for D6. Please fix it and make it work. I cannot use query strings in url aliases. When i have a "?" in the url, the site gives an apache error. And when i have a "=" in my path, its not show correctly. Please, please make it work for D6.

Tanks a lot.