Avoid huge URL aliases for non-English languages

When translating the title the automatic alias module creates an alias which can be a very long text in Asian and Middle Eastern languages (Arabic, Hebrew and Persian).

To overcome this it may be better to maintain the English alias. The following code does just this. What it does is to copy the alias for the English nodes to the translated nodes.

We will add this feature to our ICanLocalize module, allowing to choose what languages to copy the aliases to.

<?php
// copy_url_alias.php

include_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

$sql = "
        SELECT
          nid
        FROM
          {node}
        WHERE
          language = 'en'"
;
        
$query = db_query ( $sql);
while (
$request = db_fetch_object ( $query ) ) {
   
$nid = $request->nid;

   
$sql = "SELECT dst FROM {url_alias} WHERE src='node/".$nid."' AND language='en'";
   
$alias = db_result(db_query($sql));
  
    if (
$alias !== FALSE) {
  
       
$sql = "SELECT nid, language FROM {node} WHERE tnid=".$nid." AND language <> 'en'";
       
$tr_query = db_query ( $sql);
        while (
$tr_request = db_fetch_object ( $tr_query ) ) {
           
$tr_nid = $tr_request->nid;
           
$lang = $tr_request->language;

           
$sql = "SELECT dst FROM {url_alias} WHERE src='node/".$tr_nid."' AND language='".$lang."'";
           
$old_alias = db_result(db_query($sql));
          
            if (
$old_alias !== FALSE) {
                if (
$alias != $old_alias) {
                    echo
$lang . ' - ' . $alias . ' - '. $old_alias . "\n";
                   
$sql = "UPDATE {url_alias} SET dst='" . $alias . "' WHERE src='node/".$tr_nid."' AND language='".$lang."'";
                   
db_query($sql);
                }
            } else {
                echo
'No alias - ' . $lang . ' - ' . $alias . "\n";
               
$sql = "INSERT INTO {url_alias} (dst, src, language) VALUES('". $alias . "', 'node/".$tr_nid."', '".$lang."')";
               
db_query($sql);
            }
        }
    }  
}
?>

Kommentare

Why?

Why exactly is keeping the English alias better? That would mean a huge hit when it comes to SEO.

This is not for all languages

This is not for all languages but only for those languages that end up with thier url encoded.
eg. Consider this English title:
Approved: kids dentals included in the medical coverage
which translates into Hebrew as:
אושר: טיפול שיניים לילדים על חשבון סל התרופות
Using this as a URL alias is fine untill you vew your site with Internet Explorer. The URL shown in Internet Explorer is:
http://www.example.com/he/%D7%90%D7%95%D7%A9%D7%A8%3A%20%D7%98%D7%99%D7%...

on another note

Translated alias could mean a better google ranking.

and please read carefully:
http://api.drupal.org/api/function/db_query/6

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options