Jump to content

Url Re-write for cms pages and categories


Recommended Posts

Hello, I tried really hard to google or do it on my own, but I did not find a solution to my problem.


Version: Prestashop 1.7
Here is what I am trying to do: 
I crated a blog category inside Design > Pages that display blog articles.

I want the url of the blog article to be like this: mywebiste.com/blog/article-1

After I have done some research I found out that I must edit the Dispatcher.php and Link.php, so they currently look like this:

Dispatcher.php

        'cms_rule' => array(
            'controller' =>    'cms',
            'rule' =>        'content/{categories:/}{id}-{rewrite}',
            'categories' => array('regexp' => '[/_a-zA-Z0-9-\pL]*'),
            'keywords' => array(
                'id' =>            array('regexp' => '[0-9]+', 'param' => 'id_cms'),
                'rewrite' =>        array('regexp' => '[_a-zA-Z0-9\pL\pS-]*'),
                'meta_keywords' =>    array('regexp' => '[_a-zA-Z0-9-\pL]*'),
                'meta_title' =>        array('regexp' => '[_a-zA-Z0-9-\pL]*'),
            ),
        ),


and my Link.php looks like this:

 

    public function getCMSLink(
        $cms,
        $alias = null,
        $ssl = null,
        $idLang = null,
        $idShop = null,
        $relativeProtocol = false
    ) {
        if (!$idLang) {
            $idLang = Context::getContext()->language->id;
        }

        $url = $this->getBaseLink($idShop, $ssl, $relativeProtocol).$this->getLangLink($idLang, null, $idShop);

        $dispatcher = Dispatcher::getInstance();
        if (!is_object($cms)) {
            if ($alias !== null && !$dispatcher->hasKeyword('cms_rule', $idLang, 'meta_keywords', $idShop) && !$dispatcher->hasKeyword('cms_rule', $idLang, 'meta_title', $idShop)) {
                return $url.$dispatcher->createUrl('cms_rule', $idLang, array('id' => (int) $cms, 'rewrite' => (string) $alias), $this->allow, '', $idShop);
            }
            $cms = new CMS($cms, $idLang);
        }

        // Set available keywords
        $params = array();
        $params['id'] = $cms->id;
        $params['rewrite'] = (!$alias) ? (is_array($cms->link_rewrite) ? $cms->link_rewrite[(int) $idLang] : $cms->link_rewrite) : $alias;

        $params['meta_keywords'] = '';
        if (isset($cms->meta_keywords) && !empty($cms->meta_keywords)) {
            $params['meta_keywords'] = is_array($cms->meta_keywords) ? Tools::str2url($cms->meta_keywords[(int) $idLang]) : Tools::str2url($cms->meta_keywords);
        }

        $params['meta_title'] = '';
        if (isset($cms->meta_title) && !empty($cms->meta_title)) {
            $params['meta_title'] = is_array($cms->meta_title) ? Tools::str2url($cms->meta_title[(int) $idLang]) : Tools::str2url($cms->meta_title);
        }

        return $url.$dispatcher->createUrl('cms_rule', $idLang, $params, $this->allow, '', $idShop);
    }

I cant get it to work no matter what I try, any help is gladly appreciated!
Thanks!

Edited by latt13 (see edit history)

Share this post


Link to post
Share on other sites

Yes there you can influence how your links are build. But it will change all links to pages - if you want it to be specific only for "blog" category you need to implement some logic.

 

image.png

Edited by JBW (see edit history)

Share this post


Link to post
Share on other sites
4 minutes ago, JBW said:

Yes there you can influence how your links are build. But it will change all links to pages - if you want it to be specific only for "blog" category you need to implement some logic.

That is exactly what I am trying to do, but how?
I tried everything ,  read topics like these:

or this: https://stackoverflow.com/questions/39013727/url-link-for-custom-module-in-prestashop-1-6

But still cant make it work

Share this post


Link to post
Share on other sites
  • 4 weeks later...
On 12/10/2018 at 4:14 PM, JBW said:

can influence how your links are build. But it will change all link

Hello,

 

If your blog articles are all linked to the "blog" CMS category you have to use a module for this => https://addons.prestashop.com/fr/url-redirections/19643-advanced-seo-friendly-urls.html

 

I didn't use this module but for CMS page it may be possible with option "Enable parent categories in URL" to have an URL structure like this " .../CMS category / CMS page" ie ".../blog/your-blog-page".

 

Best regards,

 

AM

Share this post


Link to post
Share on other sites
  • 5 months later...
  • 7 months later...

Hello, this post helped me a lot but I have another concern about the canonical url link.
For this method, everything works except the canonical link, we should have for the canonical link: monsite.com/rewrite_produit
that's what I'm being asked, but with the modifications above it doesn't work.
I'm also asked to keep this structure for the categories in the url: either monsite.com/categories/rewrite

Has anyone ever had this problem before? I know it's specific, but I've been stuck on this for days...

Thank you so much for your help.

Share this post


Link to post
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...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More