0

i'm creating a drupal site that should include a feature that will turn a clean url into a a url with a query string.

The way it should work would be:

  1. user would type any clean url like www.example.com/hobbies/skiing in the url bar.
  2. here is my problem:

inside the .htaccess i've put this line of code:

RewriteRule ^hobbies/([a-zA-Z0-9-]*) /index.php?hobbies=$1 [NC]

in the drupal, i've created a page with PHP Filter enabled, then typed in

<?PHP echo $_GET['hobbies']; ?>

the RewriteRule should turn www.example.com/hobbies/skiing into www.example.com/index.php?hobbies=skiing but i guess the code doesn't work as expected, or drupal has codes running that either skips the .htaccess command or something.

3.once the url has been translated into a dirty url, the page will display what the value of hobbies is, as the code works when you actually type www.example.com/index.php?hobbies=skiing directly into the url bar.

can you help me with this one?

bonbon.langes
  • 1,718
  • 2
  • 22
  • 37

1 Answers1

0

There's a far easier way to do it, Drupal already keeps path parts aside for later which you can access using the arg() function:

if (arg(0) == 'hobbies' && arg(1)) {
  $hobby = arg(1);
}

If your path is an alias though arg() will return the original router path (node/1 etc.) so you'll have to be a bit more inventive:

$parts = explode('/', $_SERVER['REQUEST_URI']);
if ($parts[1] == 'hobbies' && isset($parts[2])) {
  $hobby = $parts[2];
}

UPDATE

From your comments you'd be better off making a quick custom module for this:

// Implements hook_menu().
function mymodule_menu() {
  $items['hobbies/%'] = array(
    'title' => 'A title',
    'access arguments' => array('access content'),
    'page callback' => 'mymodule_page',
    'page arguments' => array(1)
  );

  return $items;
}

function mymodule_page($hobby) {
  return $hobby;
}

That will literally print out what ever is after hobbies/ in the content area of the page

Clive
  • 36,918
  • 8
  • 87
  • 113
  • hi @kiamlaluno. where can i find or modify it, or put the codes? what if hobbies/skiing (or anything that i'd like to type) is not yet present, can i still get that value? thanks for the big help! – bonbon.langes Oct 25 '11 at 15:25
  • in case you also know the answer.. where can i find or modify it, or put the codes? what if hobbies/skiing (or anything that i'd like to type) is not yet present, can i still get that value? thanks for the big help! – bonbon.langes Oct 25 '11 at 15:25
  • Wherever you were planning to use `$_GET['hobbies']`, use the above code instead :-) – Clive Oct 25 '11 at 15:27
  • i'm talking about pages (hobbies/) that have the possibility not to exist yet. something like, creating a dynamic page and the words typed inside the neat url will be used as variables. bunch of thanks! – bonbon.langes Oct 25 '11 at 15:28
  • No, it is a bit confusing but [`arg()`](http://api.drupal.org/api/drupal/includes--bootstrap.inc/function/arg/7) is actually a Drupal function used to get URL parts, it's nothing to do with PHP's `func_get_args()` or arguments passed to a calling function – Clive Oct 25 '11 at 15:41
  • isn't arg(0) and arg(1) supposed to be arg[0] and arg[1]? (OK this one passed. the problem now is that arg(0) is equal to 'node' and arg(1) refers to the node number of tha page. hmmmm... ) – bonbon.langes Oct 25 '11 at 15:42
  • i think i'm getting nearer to my with the args(). the only problem is where to place it in order to display the right result. – bonbon.langes Oct 25 '11 at 15:44
  • wow, i'm really getting nearer! when i type in www.example.com/hobbies (the page with an alias of 'hobbies' that also includes the php code echo $parts[1]), i get the output right, which is 'hobbies'. (wow thanks for the REQUEST_URI, i learned something new). but when i type in www.example.com/hobbies/skiing, that's where i get a 'page-not-found' error. i think i need to place the code before the code that triggers 404 error to check for the url given, parse it (like the one you did in the code) so that it get's redirected to the hobbies page and print the right word after the slash. – bonbon.langes Oct 25 '11 at 15:58
  • I've added another way you could do this, check the answer :-) – Clive Oct 25 '11 at 16:33
  • awesome awesome! :D now i only need to learn how to create a new module. thanks a lot! :D – bonbon.langes Oct 25 '11 at 17:08