opencart seo friendly urls
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 like something like /product/1/girls-gone-wild
change catalog/model/tool/seo_url.php
public function rewrite($link) { if ($this->config->get('config_seo_url')) { $url_data = parse_url(str_replace('&', '&', $link)); $url = ''; $data = array(); parse_str($url_data['query'], $data); switch($data['route']) { case 'product/product': $this->load->model('catalog/product'); $product = $this->model_catalog_product->getProduct($data['product_id']); $url = $this->config->get('config_url').'product/'.$data['product_id'].'/'.$this->toSlug($product['name']); break; case 'product/category': $this->load->model('catalog/category'); $categoryId = explode('_',$data['path']); $categoryId = $categoryId[(count($categoryId)-1)]; $category = $this->model_catalog_category->getCategory($categoryId); $url = $this->config->get('config_url').'category/'.$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; } if(!empty($url)) { 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) { $name = str_replace("'", "", $name); $name = str_replace('"', "", $name); $name = strtolower($name); $name = preg_replace("/&#?[a-z0-9]+;/i","",$name); $name = preg_replace('/[^a-zA-Z0-9-]/', '-', $name); $name = preg_replace('/-+/', "-", $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_']); if($parts[0] == 'category' || $parts[0] == 'product' || $parts[0] == 'information') { switch($parts[0]) { case 'category': $this->request->get['path'] = $parts[1]; break; case 'product': $this->request->get['product_id'] = $parts[1]; break; case 'information': $this->request->get['information_id'] = $parts[1]; 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']); } } }




