How can I create custom SEO-friendly URLs in OpenCart?
It turns out this can be done with a relatively simple change to a single file. No .htaccess rewrite rules, simply patch the catalog/controller/common/seo_url.php file and add your pretty URLs to an existing database table.
The patch to seo_url.php:
Index: catalog/controller/common/seo_url.php
===================================================================
--- catalog/controller/common/seo_url.php (old)
+++ catalog/controller/common/seo_url.php (new)
@@ -48,7 +42,12 @@
$this->request->get['route'] = 'product/manufacturer/product';
} elseif (isset($this->request->get['information_id'])) {
$this->request->get['route'] = 'information/information';
- }
+ } else {
+ $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($this->request->get['_route_']) . "'");
+ if ($query->num_rows) {
+ $this->request->get['route'] = $query->row['query'];
+ }
+ }
if (isset($this->request->get['route'])) {
return $this->forward($this->request->get['route']);
@@ -88,7 +87,15 @@
}
unset($data[$key]);
- }
+ } else {
+ $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $this->db->escape($data['route']) . "'");
+
+ if ($query->num_rows) {
+ $url .= '/' . $query->row['keyword'];
+
+ unset($data[$key]);
+ }
+ }
}
}
There are two edits required. The first extends the index()
function to look in the url_alias
table for any keyword matching $this->request->get['_route_']
.
The second extends the rewrite()
function to look in the url_alias
table for all routes, not just those for products, manufacturers, and information pages.
Adding entries to the database:
INSERT INTO `url_alias` (`url_alias_id`, `query`, `keyword`) VALUES
(NULL, 'checkout/cart', 'cart');
That's it. http://example.com/cart should return the same thing that http://example.com/index.php?route=checkout/cart does, and OpenCart should recognize $this->url->link('checkout/cart');
and return a link to the pretty URL http://example.com/cart
How do I create an SEO-friendly link for custom, dynamically generated pages in Opencart?
some how manage to do it
in soe_url.php controller
elseif ($key == 'id') {
$categories[] = $value;
foreach ($categories as $category) {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = 'happy_hour_id=" . (int)$category . "'");
if ($query->num_rows && $query->row['keyword']) {
$url .= '/' . $query->row['keyword'];
} else {
$url = '';
break;
}
}
unset($data[$key]);
}
in database url_alias
query = happy_hour_id=65
keyword = happyhour/category name
in final
domain.com/happyhour/category name
opencart seo friendly url only works for some pages
After struggling a lot this is how it works:
I used this answer and this link. BUT seo_url.php
file is in catalog/controller/startup
directory.
How to remove index.php?route=extension/module/ and make SEO friendly URL for custom controller in opencart
1- Login to your Administrator account.
2- Go to: Design -> SEO URL
3- Click on the "Add" button on the top right corner
4- In the query field, type in: extension/module/[your-module-name]
5- In the Keyword field, type the SEO keywords you want
6- Click "Save".
7- Repeat that for every language and store you have.
Related Topics
Detecting a Url Using Preg_Match? Without Http:// in the String
Interface Implementation: Declaration Must Be Compatible
$_Post Is Empty After Form Submit
How to Do Aes256 Decryption in PHP
Unsetting Array Values in a Foreach Loop
Laravel - Model Class Not Found
Getting Output and Exit Status from Shell_Exec()
How to Pass a PHP Variable to Vue Component Instance in Laravel Blade
Heredoc Interfering with Code Indentation
Capture Newline from a Textarea Input
Php: Split Multibyte String (Word) into Separate Characters
How to Create a Facade Class with Laravel
Pdo Returning Incorrect, But Duplicate, Data. Key's Not in Database