OC 1.5.6分类+厂商组合SEO URL配置问题求助
Let's walk through the complete solution to get those clean /bikes/honda or /cars/bmw URLs working with your vendor filter module. The key is to tie OpenCart's SEO URL system to your vendor filtering logic, since manual url_alias entries alone won't handle the dynamic routing.
Step 1: Add Vendor Aliases to the url_alias Table
First, we need to map each vendor to a SEO-friendly keyword. Run these SQL queries (adjust vendor IDs and keywords to match your store):
INSERT INTO `url_alias` (`query`, `keyword`) VALUES ('vendor_id=3', 'honda'), -- Replace 3 with your actual Honda vendor ID ('vendor_id=5', 'bmw'); -- Replace 5 with your actual BMW vendor ID
This tells OpenCart that honda corresponds to vendor_id=3, just like your category aliases map to category paths.
Step 2: Modify catalog/controller/common/seo_url.php
This file handles both parsing incoming SEO URLs and generating outgoing ones. We'll update two functions here:
Update the index() Function (Parse Incoming URLs)
Add this code right after the line where $parts = explode('/', str_replace('../', '', $url)); to detect and handle the /category/vendor pattern:
// Handle category/vendor SEO URLs if (count($parts) == 2) { // Look up the category alias first $category_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($parts[0]) . "'"); if ($category_query->num_rows) { $category_parts = explode('=', $category_query->row['query']); if ($category_parts[0] == 'path') { // Look up the vendor alias $vendor_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($parts[1]) . "'"); if ($vendor_query->num_rows) { $vendor_parts = explode('=', $vendor_query->row['query']); if ($vendor_parts[0] == 'vendor_id') { // Set the route and parameters for your vendor filter $this->request->get['route'] = 'product/category'; // Adjust if your module uses a custom route $this->request->get['path'] = $category_parts[1]; $this->request->get['vendor_id'] = $vendor_parts[1]; // Adjust if your module uses a different parameter name // Stop further processing since we've matched the URL pattern unset($this->request->get['_route_']); return; } } } } }
Note: If your vendor filter uses a custom route (like module/vendor_filter) instead of product/category, update the route value here to match.
Update the rewrite() Function (Generate Outgoing URLs)
Replace the existing rewrite() function with this modified version to generate clean URLs when linking to filtered category pages:
public function rewrite($link) { $url_info = parse_url(str_replace('&', '&', $link)); $url = ''; $data = array(); parse_str($url_info['query'], $data); $category_keyword = ''; $vendor_keyword = ''; // Generate category/vendor SEO URL if both parameters exist if (isset($data['route']) && $data['route'] == 'product/category' && isset($data['path']) && isset($data['vendor_id'])) { // Get category alias $category_query = $this->db->query("SELECT keyword FROM " . DB_PREFIX . "url_alias WHERE `query` = 'path=" . (int)$data['path'] . "'"); if ($category_query->num_rows) { $category_keyword = $category_query->row['keyword']; } // Get vendor alias $vendor_query = $this->db->query("SELECT keyword FROM " . DB_PREFIX . "url_alias WHERE `query` = 'vendor_id=" . (int)$data['vendor_id'] . "'"); if ($vendor_query->num_rows) { $vendor_keyword = $vendor_query->row['keyword']; } // Build the clean URL if both aliases exist if ($category_keyword && $vendor_keyword) { $url = '/' . $category_keyword . '/' . $vendor_keyword; unset($data['path'], $data['vendor_id'], $data['route']); } } // Fall back to default SEO URL handling if we didn't match the category/vendor pattern if (!$url) { foreach ($data as $key => $value) { if (isset($data['route'])) { if (($data['route'] == 'product/product' && $key == 'product_id') || (($data['route'] == 'product/category' || $data['route'] == 'information/information') && $key == 'path')) { $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $this->db->escape($key . '=' . $value) . "'"); if ($query->num_rows) { $url .= '/' . $query->row['keyword']; unset($data[$key]); } } elseif ($key == 'route') { unset($data[$key]); } else { $url .= '/' . $value; } } } if ($url) { $url = '/' . trim($url, '/'); } } // Add remaining parameters as query string if needed if (!empty($data)) { $url .= '?' . http_build_query($data); } return $url_info['scheme'] . '://' . $url_info['host'] . (isset($url_info['port']) ? ':' . $url_info['port'] : '') . str_replace('/index.php', '', $url_info['path']) . $url; }
Step 3: Verify .htaccess Configuration
Ensure your root .htaccess has the standard OpenCart rewrite rules (this should already be in place, but double-check):
RewriteEngine On RewriteBase / RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L] RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L] RewriteRule ^download/(.*) /index.php?route=error/not_found [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]
This ensures incoming SEO URLs are passed to OpenCart's index.php for processing.
Step 4: Test and Troubleshoot
- Clear OpenCart Cache: Go to
System > Maintenance > Cacheand clear all cached data. - Test the URL: Visit
/bikes/hondain your browser—you should see the filtered category page. - Check Generated Links: Verify that links from your vendor filter module now use the clean SEO URLs instead of query strings.
If you hit a 404 error:
- Confirm
mod_rewriteis enabled on your server. - Double-check your
url_aliasentries for typos. - Ensure the route and parameter names in
seo_url.phpmatch what your vendor filter module uses.
内容的提问来源于stack exchange,提问作者Bux




