如何在 OpenCart 中创建自定义的 SEO 友好 URL? [英] How can I create custom SEO-friendly URLs in OpenCart?
问题描述
如何在 OpenCart 中自定义系统 URL?例如,我想要 http://example.com/index.php?route=checkout/cart 显示为 http://example.com/cart
我知道 OpenCart 为产品、类别、制造商和信息页面提供了 SEO URL,但它看起来没有内置任何其他内容(至少在 1.5.0 版之前).
事实证明,这可以通过对单个文件进行相对简单的更改来完成.没有 .htaccess 重写规则,只需修补 catalog/controller/common/seo_url.php 文件并将您漂亮的 URL 添加到现有数据库表中.
<小时>seo_url.php 的补丁:
索引:catalog/controller/common/seo_url.php====================================================================--- 目录/控制器/common/seo_url.php(旧)+++ 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'] = '信息/信息';- }+ } 其他 {+ $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'])) {返回 $this->forward($this->request->get['route']);@@ -88,7 +87,15 @@}未设置($data[$key]);- }+ } 其他 {+ $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
表中查找 any 关键字匹配 $this->request->;get['_route_']
.
第二个扩展了 rewrite()
函数以在 url_alias
表中查找所有路由,而不仅仅是产品、制造商、和信息页面.
向数据库添加条目:
INSERT INTO `url_alias` (`url_alias_id`, `query`, `keyword`) 值(NULL, '结帐/购物车', '购物车');
<小时>
就是这样.http://example.com/cart 应该返回与 http://example.com/index.php?route=checkout/cart 可以,OpenCart 应该识别 $this->url->link('checkout/cart');
并返回一个链接到漂亮的 URL http://example.com/cart>
How can you customize system URLs in OpenCart? For example, I would like http://example.com/index.php?route=checkout/cart to be displayed as http://example.com/cart
I know OpenCart provides SEO URLs for products, categories, manufacturers and information pages, but it doesn't look like there is anything built-in (at least prior to version 1.5.0) for anything else.
It turns out this can be done with a relatively simple change to a single file. No .htaccess rewrite rules, simply patch the catalog/controller/common/seo_url.php file and add your pretty URLs to an existing database table.
The patch to 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]);
+ }
+ }
}
}
There are two edits required. The first extends the index()
function to look in the url_alias
table for any keyword matching $this->request->get['_route_']
.
The second extends the rewrite()
function to look in the url_alias
table for all routes, not just those for products, manufacturers, and information pages.
Adding entries to the database:
INSERT INTO `url_alias` (`url_alias_id`, `query`, `keyword`) VALUES
(NULL, 'checkout/cart', 'cart');
That's it. http://example.com/cart should return the same thing that http://example.com/index.php?route=checkout/cart does, and OpenCart should recognize $this->url->link('checkout/cart');
and return a link to the pretty URL http://example.com/cart
这篇关于如何在 OpenCart 中创建自定义的 SEO 友好 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!