title: opencart seo tweak
body: <p>Can anyone help me tweak my opencart seo_url file so that my blog entries have the seo keyword picked up from the url_alias table and displayed as mypage.co.uk/blog/post1 instead of what it is doing now which is mypage.co.uk/blog?news_id=9</p>
  
  <p>If i remove the line <code>'information/news'      =&gt; 'blog',</code> then it picks the seo keyword assigned to the blog post correctly from the table but the url displays as mypage.co.uk/post1</p>
  
  <p>The following code may help anyone that is looking to have a more complete set of seo optimised urls beyond what opencart offers.</p>
  
  <pre><code>    &lt;?php
  class ControllerCommonSeoUrl extends Controller {
          /* SEO Custom URL */
          private $url_list = array (
              'common/home'       =&gt; '',
              'checkout/cart'     =&gt; 'basket',
              'account/register'  =&gt; 'register',
                          'account/wishlist'  =&gt; 'wishlist',
                          'checkout/checkout' =&gt; 'checkout',
                          'account/login'     =&gt; 'login',
                          'product/special'   =&gt; 'special',
                          'affiliate/account' =&gt; 'affiliate',
                          'checkout/voucher'  =&gt; 'voucher',
                          'product/manufacturer' =&gt; 'brand',
                          'account/newsletter'   =&gt; 'newsletter',
                          'account/order'        =&gt; 'order',
                          'account/account'      =&gt; 'account',
                          'information/contact'  =&gt; 'contact',
                          'account/return/insert' =&gt; 'return/insert',
                          'information/sitemap'   =&gt; 'sitemap',
              'information/news'      =&gt; 'blog',
              );
          /* SEO Custom URL */
  
      public function index() {
          // Add rewrite to url class
          if ($this-&gt;config-&gt;get('config_seo_url')) {
              $this-&gt;url-&gt;addRewrite($this);
          }
  
          // Decode URL
          if (isset($this-&gt;request-&gt;get['_route_'])) {
              $parts = explode('/', $this-&gt;request-&gt;get['_route_']);
  
              foreach ($parts as $part) {
                  $query = $this-&gt;db-&gt;query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this-&gt;db-&gt;escape($part) . "'");
  
                  if ($query-&gt;num_rows) {
                      $url = explode('=', $query-&gt;row['query']);
  
                      if ($url[0] == 'product_id') {
                          $this-&gt;request-&gt;get['product_id'] = $url[1];
                      }
  
                      if ($url[0] == 'category_id') {
                          if (!isset($this-&gt;request-&gt;get['path'])) {
                              $this-&gt;request-&gt;get['path'] = $url[1];
                          } else {
                              $this-&gt;request-&gt;get['path'] .= '_' . $url[1];
                          }
                      }   
  
                      if ($url[0] == 'manufacturer_id') {
                          $this-&gt;request-&gt;get['manufacturer_id'] = $url[1];
                      }
  
                      if ($url[0] == 'information_id') {
                          $this-&gt;request-&gt;get['information_id'] = $url[1];
                      }
  
                  } else {
                      $this-&gt;request-&gt;get['route'] = 'error/not_found';   
                  }
              }
                           /* SEO Custom URL */
                           if ( $_s = $this-&gt;setURL($this-&gt;request-&gt;get['_route_']) ) {
                                   $this-&gt;request-&gt;get['route'] = $_s;
                           }/* SEO Custom URL */
  
              if (isset($this-&gt;request-&gt;get['product_id'])) {
                  $this-&gt;request-&gt;get['route'] = 'product/product';
              } elseif (isset($this-&gt;request-&gt;get['path'])) {
                  $this-&gt;request-&gt;get['route'] = 'product/category';
              } elseif (isset($this-&gt;request-&gt;get['manufacturer_id'])) {
                  $this-&gt;request-&gt;get['route'] = 'product/manufacturer/info';
              } elseif (isset($this-&gt;request-&gt;get['information_id'])) {
                  $this-&gt;request-&gt;get['route'] = 'information/information';
              }
  
              if (isset($this-&gt;request-&gt;get['route'])) {
                  return $this-&gt;forward($this-&gt;request-&gt;get['route']);
              }
          }
      }
  
      public function rewrite($link) {
          if ($this-&gt;config-&gt;get('config_seo_url')) {
              $url_data = parse_url(str_replace('&amp;amp;', '&amp;', $link));
  
              $url = ''; 
  
              $data = array();
  
              parse_str($url_data['query'], $data);
  
              foreach ($data as $key =&gt; $value) {
                  if (isset($data['route'])) {
                      if (($data['route'] == 'product/product' &amp;&amp; $key == 'product_id') || (($data['route'] == 'product/manufacturer/info' || $data['route'] == 'product/product') &amp;&amp; $key == 'manufacturer_id') || ($data['route'] == 'information/information' &amp;&amp; $key == 'information_id')) {
                          $query = $this-&gt;db-&gt;query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $this-&gt;db-&gt;escape($key . '=' . (int)$value) . "'");
  
                          if ($query-&gt;num_rows) {
                              $url .= '/' . $query-&gt;row['keyword'];
  
                              unset($data[$key]);
                          }                   
                      } elseif ($key == 'path') {
                          $categories = explode('_', $value);
  
                          foreach ($categories as $category) {
                              $query = $this-&gt;db-&gt;query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = 'category_id=" . (int)$category . "'");
  
                              if ($query-&gt;num_rows) {
                                  $url .= '/' . $query-&gt;row['keyword'];
                              }                           
                          }
  
                          unset($data[$key]);
                       }// 
                                          /* SEO Custom URL */
                                          if( $_u = $this-&gt;getURL($data['route']) ){
                                              $url .= $_u;
                                              unset($data[$key]);
                                          }/* SEO Custom URL */        
  
  
                                  }
                          }
  
              if ($url) {
                  unset($data['route']);
  
                  $query = '';
  
                  if ($data) {
                      foreach ($data as $key =&gt; $value) {
                          $query .= '&amp;' . $key . '=' . $value;
                      }
  
                      if ($query) {
                          $query = '?' . trim($query, '&amp;');
                      }
                  }
  
                  return $url_data['scheme'] . '://' . $url_data['host'] . (isset($url_data['port']) ? ':' . $url_data['port'] : '') . str_replace('/index.php', '', $url_data['path']) . $url . $query;
              } else {
                  return $link;
              }
          } else {
              return $link;
          }       
      }
          /* SEO Custom URL */
          public function getURL($route) {
                  if( count($this-&gt;url_list) &gt; 0) {
                       foreach ($this-&gt;url_list as $key =&gt; $value) {
                          if($route == $key) {
                              return '/'.$value;
                          }
                       }
                  }
                  return false;
          }
          public function setURL($_route) {
                  if( count($this-&gt;url_list) &gt; 0 ){
                       foreach ($this-&gt;url_list as $key =&gt; $value) {
                          if($_route == $value) {
                              return $key;
                          }
                       }
                  }
                  return false;
          }/* SEO Custom URL */   
  }
  ?&gt;
  </code></pre>