0.7.7 URL Alias/SEO Friendly URLs
Posted: Wed Dec 12, 2007 7:48 am
First off I'd like to say how clean and well organized the OpenCart code is. A couple of years ago I worked with osCommerce and the difference between them is like night and day.
Please note that these changes were applied to v0.7.7.
I have been searching through the forums to see how to get URL aliases/SEO friendly URLs to work. I got some hints, but no step-by-step how-to. I've finally gotten SEO friendly URLs to work on my setup and would like to share what I did.
There is no requirement for the URL alias feature to be enabled.
I was unable to see how OpenCart could convert pretty URLs into something OpenCart could understand, so I added this code (four new lines) to library/application/router.php.
After adding this code you can test it by navigating to a page and substituting [forward] slashes for the equals, ampersand and question mark characters in your browser's address bar. If it is working you will remain on the same page when you click 'Go'.
Next some work needs to be done in library/environment/url.php.
Rename the last function in the file from 'create' to 'create_ugly' and add the following replacement function wherever you wish (in url.php).
The above function was posted somewhere on the forum, however the first three lines I added.
URLs in the catalog should now be SEO friendly, but administration URLs will be unchanged.
That's it.
Though it isn't open for business, this is the site I'm bringing up (with the modifications described in this post).
http://www.eeegads.com
Please note that these changes were applied to v0.7.7.
I have been searching through the forums to see how to get URL aliases/SEO friendly URLs to work. I got some hints, but no step-by-step how-to. I've finally gotten SEO friendly URLs to work on my setup and would like to share what I did.
There is no requirement for the URL alias feature to be enabled.
I was unable to see how OpenCart could convert pretty URLs into something OpenCart could understand, so I added this code (four new lines) to library/application/router.php.
Code: Select all
if (isset($this->routes[$path[0]])) { /* existing code */
/* ... existing code ... */
} /* existing code */
else if(isset($path[1])) { /* new code */
for ($i = 0; $i < sizeof($path); $i += 2) /* new code */
$request->set($path[$i], isset($path[$i+1]) ? $path[$i+1] : NULL); /* new code */
} /* new code */
Next some work needs to be done in library/environment/url.php.
Rename the last function in the file from 'create' to 'create_ugly' and add the following replacement function wherever you wish (in url.php).
Code: Select all
function create($server, $controller, $action = NULL, $query = array()) {
if(strpos($server, "/admin/") > 0) {
return $this->create_ugly($server, $controller, $action, $query);
}
$link = 'controller/' . $controller;
if ($action) {
$link .= '/action/' . $action;
}
foreach ($query as $key => $value) {
if ($value) {
$link .= '/' . $key . '/' . urlencode($value);
}
}
if (isset($this->data[$link])) {
$link = $this->data[$link];
} else {
$link = 'index.php/' . $link;
}
return $server . $link;
}
URLs in the catalog should now be SEO friendly, but administration URLs will be unchanged.
That's it.
Though it isn't open for business, this is the site I'm bringing up (with the modifications described in this post).
http://www.eeegads.com