首页 文章

URL重写OpenCart产品SEO

提问于
浏览
9

我来自我的opencart网上商店 . Opencart本身有一个seo实现,真的很糟糕 . 我已经更新了seo实现,以便能够为多个类别使用相同的关键字,请参阅:Opencart duplicate URL keywords但这仅适用于类别 . 对于产品,我认为我只需要一个htaccess重写规则 .

原始网址如下所示:

http://domain.com/index.php?route=product/product&path=25_28_93&product_id=1759

我的网址目前看起来像这样:

http://domain.com/In-Stock/Retreaded-Tires/Agricultural?product_id=1759

如您所见,类别确实已经发生变化 .

我希望它是这样的:

http://domain.com/In-Stock/Retreaded-Tires/Agricultural/1759/1050-50R32-Mega-X-Bib

然后为了分页(在一个类别中)我有这个网址:

http://domain.com/index.php?route=product/category&path=36_70_67&page=2

我已经把它变成:

http://domain.com/Tire-Retreading/Equalizing/&page=2

但我希望这样

http://domain.com/Tire-Retreading/Equalizing/2

我的htaccess文件看起来像这样:

Options +FollowSymlinks
Options -Indexes
<FilesMatch "(?i)((\.tpl|\.ini|\.log|(?<!robots)\.txt))">
 Order deny,allow
 Deny from all
</FilesMatch>
RewriteEngine On
RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteRule ^download/(.*) /index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]

我的seo_url.php文件有点改动,如下所示:

public function index() {
    $this->load->model('catalog/category');
    // 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_']);

        // remove any empty arrays from trailing
        if (utf8_strlen(end($parts)) == 0) {
            array_pop($parts);
        }

        $categories = array();

        for ($i = 0; $i < count($parts); $i++) {
            $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($parts[$i]) . "'");

            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') {
                    $categories[$i] = $this->model_catalog_category->getCategory($url[1]);

                    if (!isset($this->request->get['path'])) {
                        $this->request->get['path'] = $categories[$i]['category_id'];
                    } else {
                        foreach ($query->rows as $row) {
                            $url = explode('=', $row['query']);
                            $category_id = $url[1];

                            $category = $this->model_catalog_category->getCategory($category_id);

                            if ($category['parent_id'] == $categories[$i - 1]['category_id']) {
                                $this->request->get['path'] .= '_' . $category['category_id'];
                            }
                        }
                    }
                }

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

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

                if ($query->row['query'] && $url[0] != 'information_id' && $url[0] != 'manufacturer_id' && $url[0] != 'category_id' && $url[0] != 'product_id') {
                    $this->request->get['route'] = $query->row['query'];
                }

            } else {
                $this->request->get['route'] = 'error/not_found';

                break;
            }
        }

        if (!isset($this->request->get['route'])) {
            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 new Action($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 && $query->row['keyword']) {
                    $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 && $query->row['keyword']) {
                        $url .= '/' . $query->row['keyword'];
                    } else {
                        $url = '';

                        break;
                    }
                }

                unset($data[$key]);
            } else  {
                $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" .$data['route'] . "'");

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

                    unset($data[$key]);
                }
            }
        }
    }

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

        $query = '';

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

            if ($query) {
                $query = '?' . str_replace('&', '&amp;', 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;
    }
}

分页代码是这样的:

class Pagination {
    public $total = 0;
    public $page = 1;
    public $limit = 20;
    public $num_links = 8;
    public $url = '';
    public $text_first = '|&lt;';
    public $text_last = '&gt;|';
    public $text_next = '&gt;';
    public $text_prev = '&lt;';

    public function render() {
        $total = $this->total;

        if ($this->page < 1) {
            $page = 1;
        } else {
            $page = $this->page;
        }

        if (!(int)$this->limit) {
            $limit = 10;
        } else {
            $limit = $this->limit;
        }

        $num_links = $this->num_links;
        $num_pages = ceil($total / $limit);

        $this->url = str_replace('%7Bpage%7D', '{page}', $this->url);

        $output = '<ul class="pagination">';

        if ($page > 1) {
            $output .= '<li><a href="' . str_replace('{page}', 1, $this->url) . '">' . $this->text_first . '</a></li>';
            $output .= '<li><a href="' . str_replace('{page}', $page - 1, $this->url) . '">' . $this->text_prev . '</a></li>';
        }

        if ($num_pages > 1) {
            if ($num_pages <= $num_links) {
                $start = 1;
                $end = $num_pages;
            } else {
                $start = $page - floor($num_links / 2);
                $end = $page + floor($num_links / 2);

                if ($start < 1) {
                    $end += abs($start) + 1;
                    $start = 1;
                }

                if ($end > $num_pages) {
                    $start -= ($end - $num_pages);
                    $end = $num_pages;
                }
            }

            for ($i = $start; $i <= $end; $i++) {
                if ($page == $i) {
                    $output .= '<li class="active"><span>' . $i . '</span></li>';
                } else {
                    $output .= '<li><a href="' . str_replace('{page}', $i, $this->url) . '">' . $i . '</a></li>';
                }
            }
        }

        if ($page < $num_pages) {
            $output .= '<li><a href="' . str_replace('{page}', $page + 1, $this->url) . '">' . $this->text_next . '</a></li>';
            $output .= '<li><a href="' . str_replace('{page}', $num_pages, $this->url) . '">' . $this->text_last . '</a></li>';
        }

        $output .= '</ul>';

        if ($num_pages > 1) {
            return $output;
        } else {
            return '';
        }
    }
}

EDIT:

我的所有页面都被重定向到http://domain.com/index.php从那里它决定了route参数中要使用的目录/文件 . 所以 route=product/product 告诉你去目录产品中的 product.php . 目录产品还包含 categories.php ,其中包含一条路线: route=product/category

字符串中的Path变量表示类别的ID . 在我的例子中 25 代表 In-Stock . 并且 25_28 代表 In-Stock/Retreaded-Tires 等 .

product_id 变量表示产品的相应ID .

页面变量表示分页,并在类别中的产品列表中使用 . 此列表可以具有可变长度,因为它计算一个类别中的产品数量以及他应该在一页上显示的数量 . 因此,如果原始网址为 &page=2 且路由为 route=product/category ,则应该创建一个这样的网址: http://domain.com/Tire-Retreading/Equalizing/2

1 回答

  • 2

    我仍然觉得这些额外的解释令人困惑,听起来像是在试图解释 some URL rewriting basics .

    使用URL重写可以执行的操作是 translate 'pretty' URL 段(显示给用户的URL) into into 段(使用变量来提供某些内容) . 您可以通过使用正则表达式匹配漂亮的段,然后为PHP变量提供预定义值或匹配值 .

    因此,您首先要做的是弄清楚您的漂亮网址应该如何显示,以及它们各自的细分如何转化为您的变量 . (虽然您当然也可以使用任意数量的新变量,然后可以将其值转换并分配给PHP脚本中已存在或预定义的变量 . )

    你举了这个例子:http://example.com/Tire-Retreading/Equalizing/2

    此URL似乎由要转换为变量的三个部分组成:

    • Tire-Retreading (变量 route

    • Equalizing (变量 path

    • 2 (变量 page

    您必须根据各个段可拼写的所有可能方式构建您的RegEx以匹配您的细分,包括允许的任何特殊字符等 . 完成后,您可以使用反向引用将匹配的值传递给变量(或者您可以定义自己的值以供变量使用) .

    使用反向引用,您可以使用:

    RewriteRule ^([A-Za-z-]+)/([A-Za-z-]+)/([0-9]+)$ index.php?route=$1&path=$2&page=$3 [NC,L]
    

    您必须将用于段匹配的单个RegEx(如 [A-Za-z-]+ )放在圆括号中,以便能够以 $1$2 等形式将结果分配给您的php变量 .

    根据用户是否也应该被允许浏览您的产品/类别或路径的“概述”页面,您可能必须从最小的“漂亮”URL开始重写到最长的URL .

    例如 .

    RewriteRule ^([A-Za-z-]+)$ $1/ [NC,R]
    RewriteRule ^([A-Za-z-]+)/$ index.php?route=$1 [NC,L]
    RewriteRule ^([A-Za-z-]+)/([A-Za-z-]+)$ $1/$2/ [NC,R]
    RewriteRule ^([A-Za-z-]+)/([A-Za-z-]+)/$ index.php?route=$1&path=$2 [NC,L]
    RewriteRule ^([A-Za-z-]+)/([A-Za-z-]+)/([0-9]+)$ $1/$2/$3/ [NC,R]
    RewriteRule ^([A-Za-z-]+)/([A-Za-z-]+)/([0-9]+)/$ index.php?route=$1&path=$2&page=$3 [NC,L]
    

    域重写在所有“路径”重写之前进行 .

    你'll also have to figure out which flags – the part in square brackets at the end – you need to use, meaning you'将需要更详细地阅读URL rewriting . 在上面的示例中,用户键入的所有不以斜杠结尾的URL会自动重定向到(即方括号中的 R 代表的)同一URL但添加了斜杠 . 所以, http://example.com/blah 会重定向到 http://example.com/blah/ 等 .

    根据您提供的信息,您将拥有以两个可能的细分中的一个开头的网址:

    http://example.com/In-Stock/...
    

    http://example.com/Product-or-Category/....
    

    所以,你感到困惑 . 如果 In-Stock 被汇集到它自己的变量中并且总是拼写完全相同,那么你可能希望让你的第一个规则处理它并使用RegEx仅在以后匹配产品/类别(所有重写规则按时间顺序处理,一个接一个地) .

    我希望有所帮助 .

相关问题