Еще раз привет. Искал модули для того, чтобы адрес корзины был не кривой /index.php?route=checkout/cart, а просто /cart/
Единственное бесплатное решение на 2.2., которое сразу заработало — вот это http://www.opencart.com/index.php?route=extension/extension/info&extension_id=26068
Модуль состоит всего из одного файла, который переписывает файл /catalog/controller/startup/seo_url.php
Все получилось круто, но две проблемы возникло:
На несуществующих страницах перестала выводится ошибка 404, просто главную показывает с кодом 200. На конце всех ссылок сайта выводит .html.
Подскажите, как можно эти проблемы решить?
Вот код файла:
<?php
class ControllerStartupSeoUrl extends Controller {
/***************************
***** SeoUrl Functions *****
***************************/
private $urlFriendly = array(
'common/home' => 'index',
'account/register' => 'create-account',
'account/login' => 'login',
'account/logout' => 'logout',
'account/newsletter' => 'newsletter',
'account/wishlist' => 'wishlist',
'account/order' => 'order-history',
'account/account' => 'my-account',
'account/forgotten' => 'forgot-password',
'account/download' => 'downloads',
'account/return' => 'returns',
'account/transaction' => 'transactions',
'account/password' => 'change-password',
'account/edit' => 'edit-account',
'account/address' => 'address-book',
'account/reward' => 'reward-points',
'account/return/add' => 'request-add',
'account/voucher' => 'voucher',
'information/contact' => 'contact',
'information/contact/success' => 'contact-success',
'information/sitemap' => 'sitemap',
'affiliate/register' => 'create-affiliate-account',
'affiliate/login' => 'affiliate-login',
'affiliate/logout' => 'affiliate-logout',
'affiliate/account' => 'affiliates',
'affiliate/edit' => 'edit-affiliate-account',
'affiliate/password' => 'change-affiliate-password',
'affiliate/payment' => 'affiliate-payment-options',
'affiliate/tracking' => 'ffiliate-tracking-code',
'affiliate/transaction' => 'affiliate-transactions',
'affiliate/forgotten' => 'affiliate-forgot-password',
'checkout/cart' => 'shopping-cart',
'checkout/checkout' => 'checkout',
'checkout/voucher' => 'gift-vouchers',
'product/special' => 'specials',
'product/manufacturer' => 'brands',
'product/compare' => 'compare-products',
'product/search' => 'search',
);
public function getKeyFriendly($_route) {
if( count($this->urlFriendly) > 0 ){
$key = array_search($_route, $this->urlFriendly);
if($key && in_array($_route, $this->urlFriendly)){
return $key;
}
}
return false;
}
public function getValueFriendly($route) {
if( count($this->urlFriendly) > 0) {
if(in_array($route, array_keys($this->urlFriendly))){
return '/'.$this->urlFriendly[$route];
}
}
return false;
}
/***************************
***** SeoUrl Functions *****
***************************/
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_'])) {
/* SeoUrl remove prefix (*.html) */
$this->request->get['_route_'] = explode('.', $this->request->get['_route_'], -1);
$this->request->get['_route_'] = implode('.',$this->request->get['_route_']);
/* SeoUrl remove prefix (*.html) */
$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';
}
}
/* SeoUrl getKeyFriendly */
if ($_key = $this->getKeyFriendly($this->request->get['_route_']) ) { $this->request->get['route'] = $_key; }
/* SeoUrl getKeyFriendly */
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]);
}
/* SeoUrl getValueFriendly */
if( $_link = $this->getValueFriendly($data['route']) ) { $url .= $_link; unset($data[$key]); }
/* SeoUrl getValueFriendly */
}
}
if ($url) {
unset($data['route']);
$query = '';
if ($data) {
foreach ($data as $key => $value) {
$query .= '&' . rawurlencode((string)$key) . '=' . rawurlencode((is_array($value) ? http_build_query($value) : (string)$value));
}
if ($query) {
$query = '?' . str_replace('&', '&', trim($query, '&'));
}
}
/* SeoUrl add prefix (*.html) */
return $url_info['scheme'] . '://' . $url_info['host'] . (isset($url_info['port']) ? ':' . $url_info['port'] : '') . str_replace('/index.php', '', $url_info['path']) . $url . '.html' . $query;
} else {
return $link;
}
}
}
В первую очередь, я пробовал менять .html на / в этом блоке:
/* SeoUrl add prefix (*.html) */
return $url_info['scheme'] . '://' . $url_info['host'] . (isset($url_info['port']) ? ':' . $url_info['port'] : '') . str_replace('/index.php', '', $url_info['path']) . $url . '.html' . $query;
} else {
return $link;
}
Но меняются только ссылки (что, кстати, тоже очень круто, потому что мы избавляемся от дублей), но вот страницы упорно открываются только с путем .html.