如何在OpenCart中创建自定义的SEO友好URL?

26

1
试试这个vqmod,我用第二个答案的代码进行了一些编辑以修复一个小错误:http://pastebin.com/yAsfxqTm - Dreanmer
8个回答

27

事实证明,可以通过对单个文件进行相对简单的更改来完成此操作。没有.htaccess重写规则,只需对catalog/controller/common/seo_url.php文件进行修补,并将您的漂亮网址添加到现有的数据库表中。


对seo_url.php的修补:

Index: catalog/controller/common/seo_url.php
===================================================================
--- catalog/controller/common/seo_url.php   (old)
+++ catalog/controller/common/seo_url.php   (new)
@@ -48,7 +42,12 @@
                $this->request->get['route'] = 'product/manufacturer/product';
            } elseif (isset($this->request->get['information_id'])) {
                $this->request->get['route'] = 'information/information';
-           }
+           } else {
+                $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($this->request->get['_route_']) . "'");
+                if ($query->num_rows) {
+                    $this->request->get['route'] = $query->row['query'];
+                }
+           }

            if (isset($this->request->get['route'])) {
                return $this->forward($this->request->get['route']);
@@ -88,7 +87,15 @@
                        }

                        unset($data[$key]);
-                   }
+                   } else {
+                        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $this->db->escape($data['route']) . "'");
+
+                        if ($query->num_rows) {
+                            $url .= '/' . $query->row['keyword'];
+
+                            unset($data[$key]);
+                        }
+                   }
                }
            }

需要进行两次编辑。第一次是将index()函数扩展,使其在url_alias表中查找匹配$this->request->get['_route_']任何关键字。

第二个编辑是将rewrite()函数扩展,使其在url_alias表中查找所有路由,而不仅仅是产品、制造商和信息页面的路由。


向数据库添加条目:

INSERT INTO `url_alias` (`url_alias_id`, `query`, `keyword`) VALUES
(NULL, 'checkout/cart', 'cart');
那就是这样。 http://example.com/cart 应该返回与 http://example.com/index.php?route=checkout/cart 相同的内容,而 OpenCart 应该识别 $this->url->link('checkout/cart'); 并返回到漂亮的 URL 链接 http://example.com/cart

这个很好用,非常感谢。只是制造商的索引有缺陷。 - Martin Zeitler
事实证明...这是在/template/product/manufacturer_list.tpl的第12行硬编码。 - Martin Zeitler
你要把它添加到哪里? - Mr.Flocker
1
请问您能否粘贴整个控制器文件?这样比尝试理解^^^行号和其他内容要容易得多。看起来这是完美的方法,如果您能粘贴整个ControllerCommonSeoUrl类文件,我将不胜感激。谢谢! - Daniel Sikes
我正在使用OC 2.0.2.0。我尝试了您的代码并在数据库中添加了条目,但它没有起作用。请检查http://pastebin.com/DVWH7sby。这是正确的方式吗? - Prashant Tapase

15

我正在使用Opencart 1.5.5.1版本,以下是对我有效的确切代码:

<?php 
class ControllerCommonSeoUrl extends Controller {
    /* SEO Custom URL */
    private $url_list = array (
        'common/home'       => '',
        'checkout/cart'     => 'cart',
        'account/register'  => 'register',
                    'account/wishlist'  => 'wishlist',
                    'checkout/checkout' => 'checkout',
                    'account/login'     => 'login',
                    'product/special'   => 'special',
                    'affiliate/account' => 'affiliate',
                    'checkout/voucher'  => 'voucher',
                    'product/manufacturer' => 'brand',
                    'account/newsletter'   => 'newsletter',
                    'account/order'        => 'order',
                    'account/account'      => 'account',
                    'information/contact'  => 'contact',
                    'account/return/insert' => 'return',
                    'information/sitemap'   => 'sitemap',
        );
    /* SEO Custom URL */

public function index() {
    // Add rewrite to url class
    if ($this->config->get('config_seo_url')) {
        $this->url->addRewrite($this);
    }

    // Decode URL
    if (isset($this->request->get['_route_'])) {
        $parts = explode('/', $this->request->get['_route_']);

        foreach ($parts as $part) {
            $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($part) . "'");

            if ($query->num_rows) {
                $url = explode('=', $query->row['query']);

                if ($url[0] == 'product_id') {
                    $this->request->get['product_id'] = $url[1];
                }

                if ($url[0] == 'category_id') {
                    if (!isset($this->request->get['path'])) {
                        $this->request->get['path'] = $url[1];
                    } else {
                        $this->request->get['path'] .= '_' . $url[1];
                    }
                }   

                if ($url[0] == 'manufacturer_id') {
                    $this->request->get['manufacturer_id'] = $url[1];
                }

                if ($url[0] == 'information_id') {
                    $this->request->get['information_id'] = $url[1];
                }   
            } else {
                $this->request->get['route'] = 'error/not_found';   
            }
        }
                    /* SEO Custom URL */
                    if ( $_s = $this->setURL($this->request->get['_route_']) ) {
                            $this->request->get['route'] = $_s;
                    }/* SEO Custom URL */

        if (isset($this->request->get['product_id'])) {
            $this->request->get['route'] = 'product/product';
        } elseif (isset($this->request->get['path'])) {
            $this->request->get['route'] = 'product/category';
        } elseif (isset($this->request->get['manufacturer_id'])) {
            $this->request->get['route'] = 'product/manufacturer/info';
        } elseif (isset($this->request->get['information_id'])) {
            $this->request->get['route'] = 'information/information';
        }

        if (isset($this->request->get['route'])) {
            return $this->forward($this->request->get['route']);
        }
    }
}

public function rewrite($link) {
    $url_info = parse_url(str_replace('&amp;', '&', $link));

    $url = ''; 

    $data = array();

    parse_str($url_info['query'], $data);

    foreach ($data as $key => $value) {
        if (isset($data['route'])) {
            if (($data['route'] == 'product/product' && $key == 'product_id') || (($data['route'] == 'product/manufacturer/info' || $data['route'] == 'product/product') && $key == 'manufacturer_id') || ($data['route'] == 'information/information' && $key == 'information_id')) {
                $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $this->db->escape($key . '=' . (int)$value) . "'");

                if ($query->num_rows) {
                    $url .= '/' . $query->row['keyword'];

                    unset($data[$key]);
                }                   
            } elseif ($key == 'path') {
                $categories = explode('_', $value);

                foreach ($categories as $category) {
                    $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = 'category_id=" . (int)$category . "'");

                    if ($query->num_rows) {
                        $url .= '/' . $query->row['keyword'];
                    }                           
                }

                unset($data[$key]);
            }
                                    /* SEO Custom URL */
                                    if( $_u = $this->getURL($data['route']) ){
                                        $url .= $_u;
                                        unset($data[$key]);
                                    }/* SEO Custom URL */       


        }
    }

    if ($url) {
        unset($data['route']);

        $query = '';

        if ($data) {
            foreach ($data as $key => $value) {
                $query .= '&' . $key . '=' . $value;
            }

            if ($query) {
                $query = '?' . trim($query, '&');
            }
        }

        return $url_info['scheme'] . '://' . $url_info['host'] . (isset($url_info['port']) ? ':' . $url_info['port'] : '') . str_replace('/index.php', '', $url_info['path']) . $url . $query;
    } else {
        return $link;
    }
}   
    /* SEO Custom URL */
    public function getURL($route) {
            if( count($this->url_list) > 0) {
                 foreach ($this->url_list as $key => $value) {
                    if($route == $key) {
                        return '/'.$value;
                    }
                 }
            }
            return false;
    }
    public function setURL($_route) {
            if( count($this->url_list) > 0 ){
                 foreach ($this->url_list as $key => $value) {
                    if($_route == $value) {
                        return $key;
                    }
                 }
            }
            return false;
    }/* SEO Custom URL */
}
?>

我在我的controller/common文件夹中的seo_url.php文件中添加了这段代码。我正在运行1.5.5.1版本。我希望在产品的URL中能够同时看到类别、子类别和产品本身。 - Vad.Gut
这太棒了,不知道为什么他们不默认包含这个。有点荒谬。 - Daniel West
这很棒,但有一些问题。因为您重写了URL,可能会出现没有查询字符串参数的情况,然后您需要在使用它之前检查$url_info['query']是否存在,然后您还需要检查$data['route']是否存在。但我仍然有一些链接的问题,比如从购物车中删除链接...我制作了一个vqmod版本的此链接:http://pastebin.com/yAsfxqTm - Dreanmer
以上代码成功将网站转换为SEO友好型,但事件无法正常工作。我正在处理以下示例:catalog/controller/account/account/after。 - prashanth padala

2
一个简单的“无代码”方法是安装vQmod。安装说明在这里:vQmod安装wiki 然后将可在此Opencart论坛帖子中获取的xml文件上传到vqmod/xml/文件夹中。
该xml包含一个相对容易阅读的脚本,类似于上面两个答案的url映射,但不修改核心文件。因此,网站更新不会使其失效。

1
将此代码放在您的index.php文件顶部。这是我找到的唯一解决方案。
switch($_SERVER["REQUEST_URI"])
{
    case '/old-url': $three01 = "/new-url"; break;
}

if($three01)
{
    header("HTTP/1.1 301 Moved Permanently"); header("Location: ".$three01); exit;
}

1
<modification>
<id>Seo All Alias</id>
<version>1.0</version>
<vqmver>2.1.7</vqmver>
<author>noname</author>
<file name="catalog/controller/common/seo_url.php"> 
    <operation>
        <search position="after" offset="2"><![CDATA[
            $this->request->get['route'] = 'information/information';
        ]]></search>
        <add><![CDATA[
        else {
            $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($this->request->get['_route_']) . "'");
            if ($query->num_rows) {
                $this->request->get['route'] = $query->row['query'];
            }
        }
        ]]></add>
    </operation>

    <operation>
        <search position="after" offset="1" index="2"><![CDATA[
            unset($data[$key]);
        ]]></search>
        <add><![CDATA[
                else {
                    $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $this->db->escape($data['route']) . "'");

                    if ($query->num_rows) {
                        $url .= '/' . $query->row['keyword'];
                        unset($data[$key]);
                    }
                }
        ]]></add>
    </operation>

</file>


这对我有用 - 在手动将新的SEO重写添加到我的数据库_url_aliases之后。 这比修改文件本身更容易。 我正在使用2.3.0.2。 - Jacob Raccuia

0

OpenCart 1.5.X 免费的 OpenCart SEO :)

您可以随意使用。类别名称 / ID / ID,以便更快地解决 SEO 问题。

<?php
class ControllerCommonSeoUrl extends Controller {
        /* SEO Custom URL */
        private $url_list = array (
            'common/home'       => 'home',
            'checkout/cart'     => 'cart',
            'account/register'  => 'register',
                        'account/wishlist'  => 'wishlist',
                        'checkout/checkout' => 'checkout',
                        'account/login'     => 'login',
                        'product/special'   => 'special',
                        'affiliate/account' => 'affiliate',
                        'checkout/voucher'  => 'voucher',
                        'product/manufacturer' => 'brand',
                        'account/newsletter'   => 'newsletter',
                        'account/order'        => 'order',
                        'account/account'      => 'account',
                        'information/contact'  => 'contact',
                        'account/return/insert' => 'return/insert',
                        'information/sitemap'   => 'sitemap',
            );
        /* SEO Custom URL */

        public function index() {
                // Add rewrite to url class
                if ($this->config->get('config_seo_url')) {
                        $this->url->addRewrite($this);
                }

                // Decode URL
                if (isset($this->request->get['_route_'])) {
                        $parts = explode('/', $this->request->get['_route_']);

                                                if ( count($parts) > 1 ) {
                                                        if ($parts[1] == 'category'){
                                                                $this->request->get['path'] = $parts[2];
                                                                for ( $i = 3 ; $i < count($parts); $i++) {      
                                                                        $this->request->get['path'] .= '_' . $parts[$i];
                                                                }
                                                        }elseif( $parts[1] == 'item' ) {
                                                                $this->request->get['product_id'] = $parts[2];
                                                        }
                                                }
                        foreach ($parts as $part) {
                                $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($part) . "'");

                                if ($query->num_rows) {
                                        $url = explode('=', $query->row['query']);

                                        if ($url[0] == 'product_id') {
                                                $this->request->get['product_id'] = $url[1];
                                        }

                                        if ($url[0] == 'category_id') {
                                                if (!isset($this->request->get['path'])) {
                                                        $this->request->get['path'] = $url[1];
                                                } else {
                                                        $this->request->get['path'] .= '_' . $url[1];
                                                }
                                        }       

                                        if ($url[0] == 'manufacturer_id') {
                                                $this->request->get['manufacturer_id'] = $url[1];
                                        }

                                        if ($url[0] == 'information_id') {
                                                $this->request->get['information_id'] = $url[1];
                                        }       
                                } else {
                                        $this->request->get['route'] = 'error/not_found';       
                                }
                        }
                        /* SEO Custom URL */
                        if ( $_s = $this->setURL($this->request->get['_route_']) ) {
                                $this->request->get['route'] = $_s;
                        }/* SEO Custom URL */

                        if (isset($this->request->get['product_id'])) {
                                $this->request->get['route'] = 'product/product';
                        } elseif (isset($this->request->get['path'])) {
                                $this->request->get['route'] = 'product/category';
                        } elseif (isset($this->request->get['manufacturer_id'])) {
                                $this->request->get['route'] = 'product/manufacturer/product';
                        } elseif (isset($this->request->get['information_id'])) {
                                $this->request->get['route'] = 'information/information';
                        }

                        if (isset($this->request->get['route'])) {
                                return $this->forward($this->request->get['route']);
                        }
                }
        }

        public function rewrite($link) {
                if ($this->config->get('config_seo_url')) {
                        $url_data = parse_url(str_replace('&amp;', '&', $link));

                        $url = ''; 

                        $data = array();

                        parse_str($url_data['query'], $data);

                        foreach ($data as $key => $value) {
                                if (isset($data['route'])) {
                                        if ( (($data['route'] == 'product/manufacturer/product' || $data['route'] == 'product/product') && $key == 'manufacturer_id') || ($data['route'] == 'information/information' && $key == 'information_id')) {
                                                $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $this->db->escape($key . '=' . (int)$value) . "'");

                                                if ($query->num_rows) {
                                                        $url .= '/' . $query->row['keyword'];

                                                        unset($data[$key]);
                                                }                                       
                                        } elseif( $key == 'product_id' ) {

                                                $url = '/shop/item/'.$value;
                                                unset($data[$key]);

                                              }elseif ($key == 'path') {

                                                $categories = explode('_', $value);
                                                $url = '/shop/category';
                                                foreach ($categories as $category) {
                                                        $url .= '/'.$category;
                                                }
                                                unset($data[$key]);

                                        }// 
                                        /* SEO Custom URL */
                                        if( $_u = $this->getURL($data['route']) ){
                                            $url .= $_u;
                                            unset($data[$key]);
                                        }/* SEO Custom URL */        


                                }
                        }

                        if ($url) {
                                unset($data['route']);

                                $query = '';

                                if ($data) {
                                        foreach ($data as $key => $value) {
                                                $query .= '&' . $key . '=' . $value;
                                        }

                                        if ($query) {
                                                $query = '?' . trim($query, '&');
                                        }
                                }

                                return $url_data['scheme'] . '://' . $url_data['host'] . (isset($url_data['port']) ? ':' . $url_data['port'] : '') . str_replace('/index.php', '', $url_data['path']) . $url . $query;
                        } else {
                                return $link;
                        }
                } else {
                        return $link;
                }               
        }

        /* SEO Custom URL */
        public function getURL($route) {
                if( count($this->url_list) > 0) {
                     foreach ($this->url_list as $key => $value) {
                        if($route == $key) {
                            return '/'.$value;
                        }
                     }
                }
                return false;
        }
        public function setURL($_route) {
                if( count($this->url_list) > 0 ){
                     foreach ($this->url_list as $key => $value) {
                        if($_route == $value) {
                            return $key;
                        }
                     }
                }
                return false;
        }/* SEO Custom URL */
}
?>

1
重复,信息较少。 - Dreanmer

0

根据您所需的定制级别,有一些可用的扩展,包括免费的。

这个适用于OpenCart 1.5和OpenCart 2,并且似乎非常可定制。但是它不支持硬编码URL部分的定制,例如“checkout/cart”或“account/wishlist”。

这里是一个针对OpenCart 2.0的vQmod(可能适用于1.5,未经测试),适用于上述扩展,并允许将这些硬编码字符串更改为任何您想要的内容,并支持多语言网站。

例如,“checkout/cart”将变为英文中的“cart”,法语中的“panier”等。


-1

这里有一个免费的扩展程序,可以添加自定义SEO URL,同时让您管理多语言URL、404错误、导入/导出和批量编辑。

SEO模块URL

嘿,在您点击下面之前,请先查看这个扩展程序。它是我的扩展程序,我知道它编码正确,并且完全满足用户的需求,而且它是免费的。

我真心希望它能帮助到某些人。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接