简体   繁体   English

drupal重定向语言url别名

[英]drupal redirect language url alias

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 例如,在我的网站上,我有:英语-> / prices / high-school-> node / 112西班牙语->(/ es)/ precios / high-school-> node / 115

When a person or search engine reaches /es/prices/high-school a 404 is returned. 当某个人或搜索引擎到达/ es / prices / highschool时,将返回404。 What I would like is to redirect /es/prices/high-school to node/115. 我想将/ es / prices / high-school重定向到node / 115。

I would like to do this in a general form, writing a module or using an existing one if possbile. 我想以一种通用的形式进行此操作,编写一个模块,或者尽可能使用现有的模块。

Thanks. 谢谢。

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. 在预处理挂钩中,我需要检查页面,剥离前缀并从原始ID中获取节点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);
                }

            }
        }
    }
}

It won't be a solution to add different url aliases for the language versions? 为语言版本添加不同的URL别名不是解决方案吗? I mean: 我的意思是:

node/112 -> /prices/high-school node/115 -> /es/precios/escuela-secundaria 节点/ 112-> /价格/高中节点/ 115-> / es / precios / escuela-secundaria

i18n module handles language based paths and redirects too. i18n模块可处理基于语言的路径并进行重定向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM