1
votes

I would like to redirect urls that are using the wrong url alias.

Example, in my site I have: English -> /prices/high-school -> node/112 Spanish -> (/es)/precios/high-school -> node/115

When a person or search engine reaches /es/prices/high-school a 404 is returned. What I would like is to redirect /es/prices/high-school to node/115.

I would like to do this in a general form, writing a module or using an existing one if possbile.

Thanks.

2

2 Answers

1
votes

I already figured it out.

In the preprocess hook I need to check the page, strip the prefix and get the node id from the original id.

See code below:

  if(current_path()=="search404")
{
    $url = request_path();
    if (startsWith($url,'es/') ||
        startsWith($url,'de/') ||
        startsWith($url,'it/') ||
        startsWith($url,'fr/') )
    {
        $originalPath = substr($url,3,strlen($url)-3);
        $path = drupal_lookup_path("source", $originalPath,"en");
        if (isset($path))
        {
            $node = menu_get_object("node", 1, $path);
            if (isset($node))
            {
                $prefix = substr($url,0,2);
                $translated_paths = translation_path_get_translations('node/' . $node->nid);
                if (isset($translated_paths) && array_key_exists ($prefix,$translated_paths))
                {
                    if (isset($_GET['destination'])) {
                        unset($_GET['destination']);
                    }
                    $new_path = $translated_paths[$prefix];
                    drupal_goto($new_path, array(),301);
                }

            }
        }
    }
}
0
votes

It won't be a solution to add different url aliases for the language versions? I mean:

node/112 -> /prices/high-school node/115 -> /es/precios/escuela-secundaria

i18n module handles language based paths and redirects too.