我想从我的opencart网店重写我的产品网址。 Opencart本身有一个真正糟糕的seo实现。我已经更新了seo实现,以便能够使用多个类别的相同关键字,请参阅:Opencart duplicate URL keywords 但这只适用于类别。对于我想要的产品,我只需要一个htaccess重写规则。URL重写OpenCart产品SEO
原始URL看起来是这样的:
http://domain.com/index.php?route=product/product&path=25_28_93&product_id=1759
我的URL看起来像这样的时刻:
http://domain.com/In-Stock/Retreaded-Tires/Agricultural?product_id=1759
正如你所看到的类别也变化了。
而且我希望它是这样的:
http://domain.com/In-Stock/Retreaded-Tires/Agricultural/1759/1050-50R32-Mega-X-Bib
然后进行分页(类别内)我有这个网址:
http://domain.com/index.php?route=product/category&path=36_70_67&page=2
我已经做了这个分为:
http://domain.com/Tire-Retreading/Equalizing/&page=2
但我想这是
http://domain.com/Tire-Retreading/Equalizing/2
我htaccess文件是这样的:
Options +FollowSymlinks
Options -Indexes
<FilesMatch "(?i)((\.tpl|\.ini|\.log|(?<!robots)\.txt))">
Order deny,allow
Deny from all
</FilesMatch>
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]
我seo_url.php文件,该文件是有点改变了这个样子的:
public function index() {
$this->load->model('catalog/category');
// 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);
}
$categories = array();
for ($i = 0; $i < count($parts); $i++) {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($parts[$i]) . "'");
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') {
$categories[$i] = $this->model_catalog_category->getCategory($url[1]);
if (!isset($this->request->get['path'])) {
$this->request->get['path'] = $categories[$i]['category_id'];
} else {
foreach ($query->rows as $row) {
$url = explode('=', $row['query']);
$category_id = $url[1];
$category = $this->model_catalog_category->getCategory($category_id);
if ($category['parent_id'] == $categories[$i - 1]['category_id']) {
$this->request->get['path'] .= '_' . $category['category_id'];
}
}
}
}
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('&', '&', $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]);
} else {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" .$data['route'] . "'");
if ($query->num_rows && $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('&', '&', 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;
}
}
分页代码如下:
class Pagination {
public $total = 0;
public $page = 1;
public $limit = 20;
public $num_links = 8;
public $url = '';
public $text_first = '|<';
public $text_last = '>|';
public $text_next = '>';
public $text_prev = '<';
public function render() {
$total = $this->total;
if ($this->page < 1) {
$page = 1;
} else {
$page = $this->page;
}
if (!(int)$this->limit) {
$limit = 10;
} else {
$limit = $this->limit;
}
$num_links = $this->num_links;
$num_pages = ceil($total/$limit);
$this->url = str_replace('%7Bpage%7D', '{page}', $this->url);
$output = '<ul class="pagination">';
if ($page > 1) {
$output .= '<li><a href="' . str_replace('{page}', 1, $this->url) . '">' . $this->text_first . '</a></li>';
$output .= '<li><a href="' . str_replace('{page}', $page - 1, $this->url) . '">' . $this->text_prev . '</a></li>';
}
if ($num_pages > 1) {
if ($num_pages <= $num_links) {
$start = 1;
$end = $num_pages;
} else {
$start = $page - floor($num_links/2);
$end = $page + floor($num_links/2);
if ($start < 1) {
$end += abs($start) + 1;
$start = 1;
}
if ($end > $num_pages) {
$start -= ($end - $num_pages);
$end = $num_pages;
}
}
for ($i = $start; $i <= $end; $i++) {
if ($page == $i) {
$output .= '<li class="active"><span>' . $i . '</span></li>';
} else {
$output .= '<li><a href="' . str_replace('{page}', $i, $this->url) . '">' . $i . '</a></li>';
}
}
}
if ($page < $num_pages) {
$output .= '<li><a href="' . str_replace('{page}', $page + 1, $this->url) . '">' . $this->text_next . '</a></li>';
$output .= '<li><a href="' . str_replace('{page}', $num_pages, $this->url) . '">' . $this->text_last . '</a></li>';
}
$output .= '</ul>';
if ($num_pages > 1) {
return $output;
} else {
return '';
}
}
}
编辑:
我的网页全部被重定向到http://domain.com/index.php 从那里,它决定哪个目录/文件从路由参数使用。 因此route=product/product
告诉去目录产品中的product.php
。目录产品还包含导致路由的categories.php
:route=product/category
字符串中的Path变量表示类别的ID。在我的示例中,25
代表In-Stock
。 25_28
代表In-Stock/Retreaded-Tires
等。
product_id
变量代表产品的相应ID。
页面变量代表分页并用于类别内的产品列表中。此列表可以具有可变长度,因为它可以计算某个类别中有多少产品以及他应该在1页上显示多少个产品。 所以,如果原始URL具有&page=2
与路线route=product/category
应该作出这样的网址:http://domain.com/Tire-Retreading/Equalizing/2
其中是重写方法,它必须位于此控制器中。 – mcklayin
重写方法添加了我的错误,我离开了那个方法。 – Baklap4
你能否澄清一下你的意思是'什么更经常使用,我应该使用?' - 在句子结尾处似乎缺少一个词,并且尚不清楚您尝试在哪个选项之间作出决定。我不熟悉Opencart,但知道我的方式.htaccess。但是,从您提供的URL中很难确定哪些部分是哪些部分(不经过您的所有代码)。你想要的URL如何分解成不同的元素? (也就是说,原始“丑陋”网址中的路线,产品,路径,类别等转化为所需的“干净”网址中的内容)?谢谢。 – Kay