Jump to content

SEO Friendly URL not working when switching Language


Recommended Posts

Hi guys,

I am currently working on a custom module and I am having a problem with the SEO friendly URLs. If I go directly to my page recepies mysite.com/si/recepti/sladice/jabolcni-strudel it works fine, but when I change the language with the prestashop default language switcher to lets say italian it displays the italian version of the recipe but my url is not mysite.com/it/recepti/desset/strudel-di-mele but it is mysite.com/it/module/recipes/recipe?category_rewrite=dessert&rewrite=strudel-di-mele 
Can someone please help me solve this. I am attaching my code:
 

 // Here is my routes method in main module file:
  public function hookModuleRoutes($params)
    {

     return [
         'recipes-recipe' => [
            'controller' => 'recipe',
            'rule' => 'recepti/{category_rewrite}/{rewrite}',
            'keywords' => [
                'category_rewrite' => [
                    'regexp' => '[_a-zA-Z0-9-\pL]*',
                    'param' => 'category_rewrite'
                ],
                'rewrite' => [
                    'regexp' => '[_a-zA-Z0-9-\pL]*',
                    'param' => 'rewrite'
                ],
            ],
            'params' => [
                'fc' => 'module',
                'module' => 'recipes',
                'controller' => 'recipe',
            ],
        ]
    }

// Here is my frontcontroller code:
 
 public $category_rewrite;
    public $rewrite;

      public function init()
    {
        parent::init();
        $this->rewrite = Tools::getValue('rewrite');
        $this->category_rewrite = Tools::getValue('category_rewrite');
    }
   public function initContent()
    {
        parent::initContent();

        $id_lang = $this->context->language->id;

        $data = Db::getInstance()->getRow("
            SELECT n.*, nl.*
            FROM "._DB_PREFIX_."recipe n
            INNER JOIN "._DB_PREFIX_."recipe_lang nl
                ON n.id_recipe = nl.id_recipe
                AND nl.id_lang = ".(int)$id_lang."
            WHERE nl.slug = '".pSQL($this->rewrite)."'
            AND n.active = 1"
        );


        if (!$data) {
            $original_post = Db::getInstance()->getRow("
                SELECT n.id_recipe
                FROM "._DB_PREFIX_."recipe n
                INNER JOIN "._DB_PREFIX_."recipe_lang nl
                    ON n.id_recipe = nl.recipe
                WHERE nl.slug = '".pSQL($this->rewrite)."'
                AND n.active = 1"
            );

            if ($original_post) {
                // Get the post in the current language
                $data = Db::getInstance()->getRow("
                    SELECT n.*, nl.*,nl.slug as post_slug, kl.slug as category_slug
                    FROM "._DB_PREFIX_."recipe n
                    INNER JOIN "._DB_PREFIX_."recipe_lang nl
                    ON n.id_recipe = nl.id_recipe
                    INNER JOIN "._DB_PREFIX_."recipe_category_lang kl
                     ON n.id_cat = kl.id_cat
                    WHERE n.id_recipe = ".(int)$original_post['id_recipe']."
                  AND nl.id_lang = ".(int)$id_lang."
                   AND kl.id_lang = ".(int)$id_lang."
                   AND n.active = 1"
                );

                if ($data) {
                  
                    Tools::redirect($this->context->link->getModuleLink(
                        'recipes',
                        'recipe',
                        ['category_rewrite' => $data['category_slug'],
                         'rewrite' => $data['post_slug']
                        ],
                        true,
                        $id_lang
                    ));
                }
            }
        }

  $alternate_links = [];
        $languages = Language::getLanguages(true);
        foreach ($languages as $language) {
            $post_in_lang = Db::getInstance()->getRow("
              SELECT nl.slug as post_slug, kl.slug as category_slug
                FROM "._DB_PREFIX_."recipe n
                INNER JOIN "._DB_PREFIX_."recipe_lang nl
                ON n.id_recipe = nl.id_recipe
                INNER JOIN "._DB_PREFIX_."recipe_category_lang kl
                ON kl.id_cat = n.id_cat
                WHERE nl.id_recipe = ".(int)$data['id_recipe']."
             AND nl.id_lang = ".(int)$language['id_lang']."
              AND kl.id_lang = ".(int)$language['id_lang']
            );

            if ($post_in_lang) {
                $alternate_links[$language['id_lang']] = [
                    'href' => $this->context->link->getModuleLink(
                        'recipes',
                        'recipe',
                        ['category_rewrite' => $post_in_lang['category_slug'],
                        'rewrite' => $post_in_lang['slug']],

                        true,
                        $language['id_lang']
                    ),
                    'iso_code' => $language['iso_code'],
                    'lang_id' => $language['id_lang'],
                    'name' => $language['name']
                ];
            }
        }
        $this->registerLanguageUrls($alternate_links);

}
 protected function registerLanguageUrls($alternate_links)
    {
        $languages = Language::getLanguages(true);
        $links = [];

        foreach ($languages as $lang) {
            if (isset($alternate_links[$lang['id_lang']])) {
                $links[$lang['id_lang']] = $alternate_links[$lang['id_lang']]['href'];
            }
        }
        $this->context->link->lang_links = $links;
    }

 

Link to comment
Share on other sites

16 minutes ago, Prestashop Addict said:

Dis you translate your front controller in SEO & url?

Do not understand what you mean if I translate front controller? If each language has its own controller? Then no, do I have to create a seperate controller for each language?

 

Link to comment
Share on other sites

5 minutes ago, Prestashop Addict said:

If you have a front controller name myslug when you go to SE & url you can edit your front module controller dans change slug url for each language

Ok, I understand for single page, but I have a lot of recipes not just the one I mentioned in my first post, I tried to add recept/{category_rewrite}/{rewrite} in SEO & URLs but I get an error, 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...