I'm using Opencart 2.x version and shop installed in /shop subdirectory.I know how to enable seo_url (.htaccess.txt -> .htaccess, set RewriteBase to /shop/ and enable SEO urls in admin panel).
我用Opencart 2。在/shop子目录中安装x版本和shop。我知道如何启用seo_url (.htaccess)。txt -> .htaccess,将RewriteBase设置为/shop/并在管理面板中启用SEO url)。
Seo urls works and my links like information/information_id=1
changed to /faq
etc.
Seo url和我的链接,如信息/information_id=1被改成/faq等。
Problem is that if I add in url_alias table records: inforamtion/contact = contact
- this url works, but on the site it still looks like /shop/index.php?route=information/contact
not as /shop/contact
问题是,如果我添加了url_alias表记录:inforamtion/contact = contact—这个url可以工作,但是在站点上它仍然看起来像/shop/index.php?路线=信息/联系/商店/接触
I supposed that it should be changed automatically, but not. All site's links for product, categories and information pages had been changed, but that added by me manually - NOT. Why?
我认为它应该自动改变,但不是。所有网站的产品、类别和信息页面的链接都已经更改,但我手动添加了。为什么?
PS: I can change it in code, but think it is not good solution cos suppose exists correct way to do what I need.
PS:我可以在代码中修改,但是我认为这不是一个好的解决方案,因为假设存在正确的方法来做我需要的事情。
2 个解决方案
#1
2
I'm not really sure if you have already got an answer to your question, but I'll leave it here anyway, since it took me a couple days to figure this one out.
我不太确定你的问题是否已经得到了答案,但我还是把它留在这里,因为我花了几天时间才弄明白这个问题。
In order to change from /index.php?route=information/contact
to just /contact
, you require two steps
为了改变/index.php?路由=信息/联系方式/联系方式,需要两个步骤
(You probably got step 1 ready already)
(您可能已经准备好了第一步)
- Perform the following inserts in your OC database. This covers all (As for 2.1.0.1, at least) the system pages that allow now SEO settings. You can change the keywords to whatever you feel more suited.
- 在OC数据库中执行以下插入操作。这涵盖了所有(至少是2.1.0.1)允许现在进行SEO设置的系统页面。您可以将关键字更改为您认为更适合的任何内容。
INSERT INTO url_alias (query, keyword) VALUES ('common/home', '');INSERT INTO url_alias (query, keyword) VALUES ('account/wishlist', 'wishlist');INSERT INTO url_alias (query, keyword) VALUES ('account/account', 'my-account');INSERT INTO url_alias (query, keyword) VALUES ('checkout/cart', 'shopping-cart');INSERT INTO url_alias (query, keyword) VALUES ('checkout/checkout', 'checkout');INSERT INTO url_alias (query, keyword) VALUES ('account/login', 'login');INSERT INTO url_alias (query, keyword) VALUES ('account/logout', 'logout');INSERT INTO url_alias (query, keyword) VALUES ('account/order', 'order-history');INSERT INTO url_alias (query, keyword) VALUES ('account/newsletter', 'newsletter');INSERT INTO url_alias (query, keyword) VALUES ('product/special', 'specials');INSERT INTO url_alias (query, keyword) VALUES ('affiliate/account', 'affiliates');INSERT INTO url_alias (query, keyword) VALUES ('checkout/voucher', 'gift-vouchers');INSERT INTO url_alias (query, keyword) VALUES ('product/manufacturer', 'brands');INSERT INTO url_alias (query, keyword) VALUES ('information/contact', 'contact-us');INSERT INTO url_alias (query, keyword) VALUES ('account/return/insert', 'request-return');INSERT INTO url_alias (query, keyword) VALUES ('information/sitemap', 'sitemap');INSERT INTO url_alias (query, keyword) VALUES ('account/forgotten', 'forgot-password');INSERT INTO url_alias (query, keyword) VALUES ('account/download', 'downloads');INSERT INTO url_alias (query, keyword) VALUES ('account/return', 'returns');INSERT INTO url_alias (query, keyword) VALUES ('account/transaction', 'transactions');INSERT INTO url_alias (query, keyword) VALUES ('account/register', 'create-account');INSERT INTO url_alias (query, keyword) VALUES ('product/compare', 'compare-products');INSERT INTO url_alias (query, keyword) VALUES ('product/search', 'search');INSERT INTO url_alias (query, keyword) VALUES ('account/edit', 'edit-account');INSERT INTO url_alias (query, keyword) VALUES ('account/password', 'change-password');INSERT INTO url_alias (query, keyword) VALUES ('account/address', 'address-book');INSERT INTO url_alias (query, keyword) VALUES ('account/reward', 'reward-points');INSERT INTO url_alias (query, keyword) VALUES ('affiliate/edit', 'edit-affiliate-account');INSERT INTO url_alias (query, keyword) VALUES ('affiliate/password', 'change-affiliate-password');INSERT INTO url_alias (query, keyword) VALUES ('affiliate/payment', 'affiliate-payment-options');INSERT INTO url_alias (query, keyword) VALUES ('affiliate/tracking', 'affiliate-tracking-code');INSERT INTO url_alias (query, keyword) VALUES ('affiliate/transaction', 'affiliate-transactions');INSERT INTO url_alias (query, keyword) VALUES ('affiliate/logout', 'affiliate-logout');INSERT INTO url_alias (query, keyword) VALUES ('affiliate/forgotten', 'affiliate-forgot-password');INSERT INTO url_alias (query, keyword) VALUES ('affiliate/register', 'create-affiliate-account');INSERT INTO url_alias (query, keyword) VALUES ('affiliate/login', 'affiliate-login');
- You need to modify the following
catalog/common/seo_url.php
file to look like the following: - 您需要修改以下目录/公共/seo_url。php文件如下:
<?phpclass ControllerCommonSeoUrl extends Controller { 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_']); // remove any empty arrays from trailing if (utf8_strlen(end($parts)) == 0) { array_pop($parts); } 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]; } 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('&', '&', $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]); } elseif ($key == 'route') { $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $value . "'"); if ($query->num_rows && ($query->row['keyword'] || $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('&', '&', 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; } }}
This will rewrite all the links to whatever SEO keyword you assigned to that route on your url_alias table.
这将重写您在url_alias表上分配给该路由的任何SEO关键字的所有链接。
And that's it! Hopefully this helps more people, because it's a pain to dive in all that code.
这是它!希望这能帮助更多的人,因为在所有的代码中埋头苦干是很痛苦的。
#2
0
Where abouts on the front-end are the non-SEO friendly URL's (the ones with index.php?route= in them) appearing? If you've copied/pasted any into markup in your templates they will remain as they are. Opencart also has various redirects and things hard-coded into it's controller files which might redirect to such a URL. So it depends where these links are on your site.
前端的哪里有非seo友好的URL(索引。php?路线=)出现?如果您在模板中复制/粘贴了任何标记,它们将保持原样。Opencart也有各种重定向和硬编码到它的控制器文件,这些文件可能重定向到这样一个URL。这取决于这些链接在你的网站上的位置。
#1
2
I'm not really sure if you have already got an answer to your question, but I'll leave it here anyway, since it took me a couple days to figure this one out.
我不太确定你的问题是否已经得到了答案,但我还是把它留在这里,因为我花了几天时间才弄明白这个问题。
In order to change from /index.php?route=information/contact
to just /contact
, you require two steps
为了改变/index.php?路由=信息/联系方式/联系方式,需要两个步骤
(You probably got step 1 ready already)
(您可能已经准备好了第一步)
- Perform the following inserts in your OC database. This covers all (As for 2.1.0.1, at least) the system pages that allow now SEO settings. You can change the keywords to whatever you feel more suited.
- 在OC数据库中执行以下插入操作。这涵盖了所有(至少是2.1.0.1)允许现在进行SEO设置的系统页面。您可以将关键字更改为您认为更适合的任何内容。
INSERT INTO url_alias (query, keyword) VALUES ('common/home', '');INSERT INTO url_alias (query, keyword) VALUES ('account/wishlist', 'wishlist');INSERT INTO url_alias (query, keyword) VALUES ('account/account', 'my-account');INSERT INTO url_alias (query, keyword) VALUES ('checkout/cart', 'shopping-cart');INSERT INTO url_alias (query, keyword) VALUES ('checkout/checkout', 'checkout');INSERT INTO url_alias (query, keyword) VALUES ('account/login', 'login');INSERT INTO url_alias (query, keyword) VALUES ('account/logout', 'logout');INSERT INTO url_alias (query, keyword) VALUES ('account/order', 'order-history');INSERT INTO url_alias (query, keyword) VALUES ('account/newsletter', 'newsletter');INSERT INTO url_alias (query, keyword) VALUES ('product/special', 'specials');INSERT INTO url_alias (query, keyword) VALUES ('affiliate/account', 'affiliates');INSERT INTO url_alias (query, keyword) VALUES ('checkout/voucher', 'gift-vouchers');INSERT INTO url_alias (query, keyword) VALUES ('product/manufacturer', 'brands');INSERT INTO url_alias (query, keyword) VALUES ('information/contact', 'contact-us');INSERT INTO url_alias (query, keyword) VALUES ('account/return/insert', 'request-return');INSERT INTO url_alias (query, keyword) VALUES ('information/sitemap', 'sitemap');INSERT INTO url_alias (query, keyword) VALUES ('account/forgotten', 'forgot-password');INSERT INTO url_alias (query, keyword) VALUES ('account/download', 'downloads');INSERT INTO url_alias (query, keyword) VALUES ('account/return', 'returns');INSERT INTO url_alias (query, keyword) VALUES ('account/transaction', 'transactions');INSERT INTO url_alias (query, keyword) VALUES ('account/register', 'create-account');INSERT INTO url_alias (query, keyword) VALUES ('product/compare', 'compare-products');INSERT INTO url_alias (query, keyword) VALUES ('product/search', 'search');INSERT INTO url_alias (query, keyword) VALUES ('account/edit', 'edit-account');INSERT INTO url_alias (query, keyword) VALUES ('account/password', 'change-password');INSERT INTO url_alias (query, keyword) VALUES ('account/address', 'address-book');INSERT INTO url_alias (query, keyword) VALUES ('account/reward', 'reward-points');INSERT INTO url_alias (query, keyword) VALUES ('affiliate/edit', 'edit-affiliate-account');INSERT INTO url_alias (query, keyword) VALUES ('affiliate/password', 'change-affiliate-password');INSERT INTO url_alias (query, keyword) VALUES ('affiliate/payment', 'affiliate-payment-options');INSERT INTO url_alias (query, keyword) VALUES ('affiliate/tracking', 'affiliate-tracking-code');INSERT INTO url_alias (query, keyword) VALUES ('affiliate/transaction', 'affiliate-transactions');INSERT INTO url_alias (query, keyword) VALUES ('affiliate/logout', 'affiliate-logout');INSERT INTO url_alias (query, keyword) VALUES ('affiliate/forgotten', 'affiliate-forgot-password');INSERT INTO url_alias (query, keyword) VALUES ('affiliate/register', 'create-affiliate-account');INSERT INTO url_alias (query, keyword) VALUES ('affiliate/login', 'affiliate-login');
- You need to modify the following
catalog/common/seo_url.php
file to look like the following: - 您需要修改以下目录/公共/seo_url。php文件如下:
<?phpclass ControllerCommonSeoUrl extends Controller { 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_']); // remove any empty arrays from trailing if (utf8_strlen(end($parts)) == 0) { array_pop($parts); } 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]; } 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('&', '&', $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]); } elseif ($key == 'route') { $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $value . "'"); if ($query->num_rows && ($query->row['keyword'] || $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('&', '&', 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; } }}
This will rewrite all the links to whatever SEO keyword you assigned to that route on your url_alias table.
这将重写您在url_alias表上分配给该路由的任何SEO关键字的所有链接。
And that's it! Hopefully this helps more people, because it's a pain to dive in all that code.
这是它!希望这能帮助更多的人,因为在所有的代码中埋头苦干是很痛苦的。
#2
0
Where abouts on the front-end are the non-SEO friendly URL's (the ones with index.php?route= in them) appearing? If you've copied/pasted any into markup in your templates they will remain as they are. Opencart also has various redirects and things hard-coded into it's controller files which might redirect to such a URL. So it depends where these links are on your site.
前端的哪里有非seo友好的URL(索引。php?路线=)出现?如果您在模板中复制/粘贴了任何标记,它们将保持原样。Opencart也有各种重定向和硬编码到它的控制器文件,这些文件可能重定向到这样一个URL。这取决于这些链接在你的网站上的位置。