opencart has built in seo friendly url but that’s for products only as far as i can tell. and you have to actually type out the terms. so i took it upon myself to make it friendlier and more automagic.
this is for opencart v1.4.8b
the format of url we are trying to achieve is:
/product/:product_id/:product_title
so it would look something like /product/1/girls-gone-wild
change catalog/model/tool/seo_url.php
UPDATED: includes all urls now and added file for download (Catalog.rar (601))
-
public function rewrite($link)
-
{
-
if ($this->config->get(‘config_seo_url’))
-
{
-
-
-
$url = ”;
-
-
-
-
-
-
switch($data[‘route’])
-
{
-
case ‘product/product’:
-
$this->load->model(‘catalog/product’);
-
$product = $this->model_catalog_product->getProduct($data[‘product_id’]);
-
//get product categories
-
$path = ”;
-
if(!isset($this->
;request
->
;get
[‘path’]) || empty($this->
;request
->
;get
[‘path’]))
-
{
-
$categories = $this->model_catalog_product->getCategories($product[‘product_id’]);
-
-
{
-
$path = $this->model_catalog_product->getPath($categories[’0′][‘category_id’]);
-
-
}
-
}
-
else
-
{
-
$path = $this->request->get[‘path’];
-
}
-
$url = $this->
;config
->
;get
(‘config_url’).‘product/’.$data[‘product_id’].‘/’.(!empty($path)?
$path.‘/’:”).$this->
;toSlug
($product[‘name’]);
-
break;
-
case ‘product/category’:
-
$this->load->model(‘catalog/category’);
-
$this->load->model(‘catalog/product’);
-
$categoryId = explode(‘_’,$data[‘path’]);
-
$path = ”;
-
if(count($categoryId) == 1
)
-
{
-
$path = $this->model_catalog_product->getPathUp($categoryId[’0′]);
-
-
}
-
-
$categoryId = $categoryId[(count($categoryId)-1
)];
-
$category = $this->model_catalog_category->getCategory($categoryId);
-
$url = $this->config->get(‘config_url’).‘category/’.($path?$path:$data[‘path’]).‘/’.$this->toSlug($category[‘name’]);
-
break;
-
case ‘information/information’:
-
$this->load->model(‘catalog/information’);
-
$info = $this->model_catalog_information->getInformation($data[‘information_id’]);
-
$url = $this->config->get(‘config_url’).‘information/’.$data[‘information_id’].‘/’.$this->toSlug($info[‘title’]);
-
break;
-
default:
-
$route = explode(‘/’,$data[‘route’]);
-
$url = $this->config->get(‘config_url’).$data[‘route’];
-
-
break;
-
}
-
-
{
-
-
unset($data[‘route’],$data[‘path’],$data[‘product_id’],$data[‘information_id’]);
-
$query = ”;
-
-
if ($data)
-
{
-
foreach ($data as $key => $value)
-
{
-
$query .= ‘&’ . $key . ‘=’ . $value;
-
}
-
-
if ($query)
-
{
-
$query = ‘?’ . trim($query, ‘&’);
-
}
-
}
-
return $url.$query;
-
}
-
return $link;
-
-
} else
-
{
-
return $link;
-
}
-
}
-
-
public function toSlug($name)
-
{
-
-
-
-
-
-
-
return $name;
-
}
then change catalog/controller/common/seo_url.php
-
public function index()
-
{
-
if (isset($this->
;request
->
;get
[‘_route_’]))
-
{
-
$parts = explode(‘/’, $this->
;request
->
;get
[‘_route_’]);
-
$routes = array(‘category’,‘product’,‘information’,‘account’,‘common’,‘checkout’);
-
-
-
{
-
switch($parts[0])
-
{
-
case ‘category’:
-
$this->request->get[‘path’] = $parts[1];
-
break;
-
case ‘product’:
-
$this->request->get[‘product_id’] = $parts[1];
-
$this->request->get[‘path’] = $parts[2];
-
break;
-
case ‘information’:
-
$this->request->get[‘information_id’] = $parts[1];
-
break;
-
default:
-
$this->request->get[‘route’] = $this->request->get[‘_route_’];
-
break;
-
}
-
}
-
else
-
{
-
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’;
-
}
-
}
-
}
-
-
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’;
-
} 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’]);
-
}
-
}
-
}