OpenCart – Apache Mod Rewrite & SEO

WHAT IS APACHE MOD REWRITE

Apache Mod Rewrite is an Apache mod that provides a rule-based rewriting engine to rewrite requested URLs on the fly. In layman’s terms: you can change the url that people see in your browser bar to something of your choice. There “are” however rules about this that should be followed especially when it comes to Search Engine Optimization and how Google handles the url’s. Most just know how to “turn SEO URL” on from the Opencart system settings but few actually know how it works and to be fair most don’t. This is for those that want a little better understanding on the process.  I find that having knowledge, is power, and gives you the ability to maximize your efficiency on the web with your online commerce store. We won’t delve into the actual code on the APACHE side but more so on the Opencart side.

Requirements:

  • You must be using APACHE to utilize the tool. By purchasing web hosting from Arvixe you can choose for a LINUX set up which comes with APACHE so it will already be included.
  • You must use a .htaccess file to call the mod-rewrite (Opencart already does this for you)

Dynamic and Static URL’s

The whole reason for using Apache Mod Rewrite is not just for looks and aesthetics even though it “does” in fact take a messy URL and clean it up. Messy url’s are better known as dynamic URL’s.

Dynamic URL example: http://www.yourdomain.com/index.php?route=products&category=sports&sport=dogers baseball

Static URL example: (cleaned up) – http://www.yourdomain.com/sports/dogers-baseball

Not only does the static URL look better and is easier to read but Google also will have a better time indexing the URL because it is static. It’s been proven times over that having a store full of thousands of dynamic URL’s does little to improve your SEO ranking so make sure to clean them up before going live!

Opencart SEO KEYWORDS

Each product, category, certain information pages and a few other pages in Opencart allow for a seo keyword. The Opencart system takes care of the rest of the URL for you so all you have to do is enter the last portion. For example if you were editing a product called “Black Beans” you will see a box called SEO KEYWORD under the DATA TAB. In that box is where you would want to enter the name “black-beans” or “black_beans”. You want to use either a dash (which is the most common method) or use an underscore but don’t leave spaces.  All of these SEO keywords are stored in a table called url_alias. In that table you have a field called QUERY and a field called KEYWORD which old the important stuff. The PHP that is in charge of rewriting is found at atalog/controller/common/seo_url.php. When opening that file you will see the following code:

<?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_']);

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/info’;
} 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']);
}
}
}

public function rewrite($link) {
if ($this->config->get(‘config_seo_url’)) {
$url_data = parse_url(str_replace(‘&amp;’, ‘&’, $link));

$url = ”;

$data = array();

parse_str($url_data['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) {
$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) {
$url .= ‘/’ . $query->row['keyword'];
}
}

unset($data[$key]);
}
}
}

if ($url) {
unset($data['route']);

$query = ”;

if ($data) {
foreach ($data as $key => $value) {
$query .= ‘&’ . $key . ‘=’ . $value;
}

if ($query) {
$query = ‘?’ . trim($query, ‘&’);
}
}

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;
}
}
}
?>

This code “while you don’t need to understand 100% of what it is doing”, is important to encoding and decoding the URL. This, along with htaccess, your new URL will show up in place of your old one so as long as you have SEO URL turned on in the system settings and so as long as you have your settings correct:

# .OPENCART .HTACCESS SEO URL Settings
RewriteEngine On
# If your opencart installation does not run on the main web folder make sure you folder it does run in ie. / becomes /shop/

RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]

Looking for quality OpenCart Hosting?  Look no further than Arvixe Web Hosting!

Tags: , , , , | Posted under OpenCart | RSS 2.0

Author Spotlight

Joe Stenhouse

Joe Stenhouse

I am a web application developer that specializes in PHP, JAVASCRIPT, MYSQL, HTML, and CSS. We manifest exciting potentials within the world wide web through means of innovation.

One Comment on OpenCart – Apache Mod Rewrite & SEO

  1. Website Development says:

    thank you, this really helped me to learn to OpenCart, I mostly use wordpress.

Leave a Reply

Your email address will not be published. Required fields are marked *


4 + = 5

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>