Is there a way to alter a URL alias without using a module? Somebody told me to write a record into database table, but I don't know how to do it.

  • 1
    That sounds like a terrible idea. – tim.plunkett Mar 30 '11 at 5:01

Drupal allows to add (and alter) a path alias using the user interface. Generally, there is no reason to alter a database table directly, especially when a Drupal core module allows to do what you are trying to achieve through an administrative page.

If you need to do it from code, then you can use path_set_alias() function as pointed out from tim.plunkett. Differently, Drupal user interface is preferable for such tasks.

The query used by path_set_alias() to update an existing alias is similar to the following (I simplified the query by removing the reference to the language field):

UPDATE {url_alias} SET src = '<alias>', dst = '<destination>' WHERE dst = '<old_destination>'

If your Drupal site is using table prefixes (e.g. dr6_) then you need to replace {url_alias} with dr6_url_alias; if your Drupal site is not using table prefixes, then replace {url_alias} with url_alias.
Replace also <alias> in the query I wrote with the path alias, <destination> with the path to alias, and <old_destination> with the old path (which can also be the same value used for <destination>).

If there isn't a path alias, then the query becomes the following one:

INSERT INTO {url_alias} (src, dst) VALUES ('<alias>', '<destination>')

<alias> and <destination> have the same meaning I reported before.

  • What is the purpose of reposting my exact answer? – tim.plunkett Mar 30 '11 at 5:42
  • i put this code in preprocess_page function, if (isset($node->path) && $node->type=='story') { path_set_alias('node/' . $node->nid, $node->path.'/test', NULL, $language); } but it doesn't work. – enjoylife Mar 30 '11 at 5:44
  • @tim.plunkett: You didn't suggest to do it through the user interface, as far as I can see. – kiamlaluno Mar 30 '11 at 5:52
  • 1
    @enjoylife: I would not suggest to alter a path alias inside hook_preprocess_page(). Is there any reason you cannot change the path alias from the user interface? Your question was asking about doing it without using a module, but from your comment I get you are using a module (whenever it is your custom module or a third-party module, it is still a module). – kiamlaluno Mar 30 '11 at 5:56

Inserting values directly into the database is a poor choice when there is an API available. The function you are looking for is path_set_alias().

If you absolutely insist on inserting rows directly into the database, the table is {url_alias}.

  • i put this code in preprocess_page function, if (isset($node->path) && $node->type=='story') { path_set_alias('node/' . $node->nid, $node->path.'/test', NULL, $language); } but it doesn't work. if i inserting rows directly into the database,how and where to write and put the code in. – enjoylife Mar 30 '11 at 5:46
  • You would only set an alias once, not on every page load. Hence, you would need to write a module. If you don't want to use a module, then go into your database directly and add another row. If you want generic paths, use Pathauto. – tim.plunkett Mar 30 '11 at 5:51
  • if i want to add the test after the node story content type. how to alter the database? – enjoylife Mar 30 '11 at 6:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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