I wrote a small snippet of code for generating automatic "speaking URL's" for taxonomy terms.
Currently this is located in settings.php and it works nice for my personal page (http://www.narres.com/).

function custom_url_rewrite($type, $path, $original) {
  $newpath = $path;

  // This path was already aliased, skip rewriting it
  if ($path != $original) {
    return $path;
  }

// Restore from custom url:
  elseif ($type == 'source') {
    if (substr($path, 0, strlen("tag/")) == "tag/") {
      $termid = substr(strrchr($path, "."), 1);
      $newpath = "taxonomy/term/".$termid;
    }
  }

// Rewrite to custom url:
  elseif ($type == 'alias') {
    if (substr($path, 0, strlen("taxonomy/term/")) == "taxonomy/term/") {
      $termid = substr($path, strlen("taxonomy/term/"));
      $termname = taxonomy_get_term($termid);
      $newpath = "tag/".$termname->name.".".$termid."/";
    }
  }

// Nothing to do:
  return $newpath;
}

This snippet rewrites an URL like http://drupal.org/taxonomy/term/44 to http://drupal.org/tag/Security+announcements.44/, which is helpfull for SEO and needs no additional database storage like pathaliasing or pathauto.module.

Why is it not possible to locate this in a module?
Is this a bug, a feature or an error of me?

Comments

yaph’s picture

The pathauto module (http://drupal.org/project/pathauto) generates friendly URLs for taxonomy terms.

---
ramiro.org

narres’s picture

cause it generates automatic aliases which are stored in url_aliases table. Once you have done this you cannot differ, which aliases you have set manually and which where generated by pathauto.module.

For huge sites this is also a performance issue.

But thanks, or in German: Danke vielmals aus Köln.

Thomas Narres
Keep the sunny side up

amnon’s picture

Nice idea, thanks for sharing.

This snippet rewrites an URL like http://drupal.org/taxonomy/term/44 to http://drupal.org/tag/Security+announcements.44/

But you could get rid of the ".44" and the slash at the end, couldn't you?

Why is it not possible to locate this in a module?

You mean, what in the design of Drupal makes it impossible to write a module that does the same thing?

it works nice for my personal page
http://www.narres.com/

You use microscopic font on your site :-(

BTW, how do you compare Drupal to Typo3? (I know it's off-topic to this thread, but it's very interesting).

narres’s picture

I knew there was a reason why i shouldn't use "Georgia" as primary font :)

"But you...":
I used the .44 at the end, cause it saves one database search which I would otherwise need to get the $tid by taxonomy_get_term_by_name();
The "/" at the end indicates, that this is an index (directory). This is due to conformance. I know nobody does it like this, but it's defined in this way.

"You mean...":
Exactly. I'm able to execute the "$type == 'alias'" part, but the "$type == 'source'" part is not working.

"Drupal to Typo3":
That's a question like: How do you compare "Word" to "Excel"? It depends on what you want to do.
I'm using Typo3 since 2000 (in fact since the first beta) and I love the real great backend.
Drupal gives me much more easy control to realize own plugins.

The main difference between these two systems is that Typo3 is more "closed black box" oriented and Drupal is a very open architecture.

In practice I would use Typo3 for a smaller coorporate site with no special functions where nonexerienced users are building the content and Drupal for more complex and specialized sites with special needs.
I fact 80% (and growing) of my sites are currently realized with Drupal, although I'm grown up with Typo3.
Resume: The bundle of Drupal and Typo3 is the best what you portfolio should contain.

Thomas Narres
Keep the sunny side up

amnon’s picture

narres, thank you for your response.

"Drupal to Typo3":
[...]
The main difference between these two systems is that Typo3 is more "closed black box" oriented and Drupal is a very open architecture.

I like Drupal, but I want to have a look at other CMSs too, to "open my mind".

I'm a perfectionist guy, and I like control, so I don't like "closed black boxes". I like Drupal because it's small, a trait which makes it easy to master it.

I'll probably have a look at Typo3 someday. I visited Typo3's site but got tired just by reading it's huge featues list... ;-)

I used the .44 at the end, cause it saves one database search

If you install the 'devel' module, you'll find out that Drupal executes tens of queries, so I don't think one more query will make a difference...

(BTW, I wonder why the 'term_data' table doesn't have an INDEX on the 'name' column.)

Dries’s picture

Make sure to escape the URLs before you use them. Otherwise your site might be vulnerable to XSS attacks. Be careful with this snippet.

narres’s picture

I was blind cause I can't get it into a module. This made me crazy, so that I published this dangerous snippet.

Thomas Narres
Keep the sunny side up

Summit’s picture

Hi,
Could somebody please rewrite this snippet (for Drupal 5) so it can be used without danger?
Thanks a lot in advance!

greetings,
Martijn

yfreeman’s picture

you would filter out user generated content such as termnames

$termname = check_plain(taxonomy_get_term($termid));

Summit’s picture

Hi,
So is this a "save" snippet now?

<?php
function custom_url_rewrite($type, $path, $original) {
$newpath = $path;

// This path was already aliased, skip rewriting it
if ($path != $original) {
return $path;
}

// Restore from custom url:
elseif ($type == 'source') {
if (substr($path, 0, strlen("tag/")) == "tag/") {
$termid = substr(strrchr($path, "."), 1);
$newpath = "taxonomy/term/".$termid;
}
}

// Rewrite to custom url:
elseif ($type == 'alias') {
if (substr($path, 0, strlen("taxonomy/term/")) == "taxonomy/term/") {
$termid = substr($path, strlen("taxonomy/term/"));
$termname = check_plain(taxonomy_get_term($termid));
$newpath = "tag/".htmlspecialchars($termname->name).".".$termid."/";}
}

// Nothing to do:
return $newpath;
}
?>
NiklasBr’s picture

I believe you have to make the check_plain() on your data in, else the XSS will have multiple possibilities to change your code. XSS like this would probably be done by sending your site a malicious URL.

I suppose $type is internal from Drupal $path is the URL from $_GET and $original is internal. Right? Therefore:

<?php
function custom_url_rewrite($type, $path, $original) {
$newpath = check_plain($path);
$clean_path = check_plain($path);

// This path was already aliased, skip rewriting it
if ($clean_path != $original) {
return $clean_path;
}

// Restore from custom url:
elseif ($type == 'source') {
if (substr($clean_path, 0, strlen("tag/")) == "tag/") {
$newpath = "taxonomy/term/". substr(strrchr($clean_path, "."), 1);
}
}

// Rewrite to custom url:
elseif ($type == 'alias') {
if (substr($clean_path, 0, strlen("taxonomy/term/")) == "taxonomy/term/") {
$termname = taxonomy_get_term(substr($clean_path, strlen("taxonomy/term/")));
$newpath = "tag/".htmlspecialchars($termname->name).".".$termid."/";}
}

// Nothing to do:
return $newpath;
}

?>
Richard_’s picture

I tried this snippet, but it doesnt work for me. It rewrites url but without termid at the end and I get "Page not found"....
If I add term id munally , tag page is triggered correctly.

Any idea why .$termid is not getting into url?

Thank you!

Summit’s picture

You are right, as you can see the $termid is not defined.
I will post a for me working version if you like?

greetings,
Martijn

Richard_’s picture

I thought so because there isnt any load_querry. But I am not a programmer, so I wasnt sure :)

Can you post your working version here? I will appreciate it :)

One more question, if I may. Have you came accros url rewrite for node names? I was searching for it, but found nothing. I have tested Pathauto, but that ads up many many queries on my homepage :( So I better go with simple url rewrite.

Thank you!
Richard

Summit’s picture

Hi,

Here is my working version. I use it for taxonomy_menu, so thats why "category" instead of "taxonomy/term" is there.
And "resultaten" is my redirect "tag".

function custom_url_rewrite($type, $path, $original) {
# vocabulary = 2
 $vid = 2 ;
 $newpath = $path;

 // This path was already aliased, skip rewriting it
 if ($path != $original) {
  return $path;
 }

// Rewrite to custom url:
 elseif ($type == 'alias') {
  if (substr($path, 0, strlen("category/")) == "category/") {
  $termid = substr($path, strlen("category/"));
  $termname = taxonomy_get_term($termid);
  $newpath = "resultaten/". htmlspecialchars($termname->name).".".$termid."/";
  }
 }
// Nothing to do:
 return $newpath;
}

It took me quite some time to work this out. If you would add a link from your site to:
www.trekking-world.com with text great trekking and hiking site I would very much appreciate it!

Pathauto is really the way to go, lots of other module are attached to this working via pathauto, and for me it is working great!

greetings,
Martijn

Richard_’s picture

Thank you Martijn, I will try this.

Pathauto is adding nearly 300 queries to my frontapage (I have many links there probably).
That is why I am searching for other options.

You are using Path and this modification along?

As for your link, I cant promise you that, my site is not in english, plus not about trekking, but I will keep it in mind and if I find good place to mention it I will do that .-)