skip to Main Content

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).

Seo urls works and my links like information/information_id=1 changed to /faq etc.

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

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.

2

Answers


  1. 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.

    Login or Signup to reply.
  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

    (You probably got step 1 ready already)

    1. 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.
    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');
    1. You need to modify the following catalog/common/seo_url.php file to look like the following:
    <?php
    class 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('&amp;', '&', $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('&', '&amp;', 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.

    And that’s it! Hopefully this helps more people, because it’s a pain to dive in all that code.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search