我想从我的opencart网店重写我的产品网址。 Opencart本身有一个seo实现,真的很糟糕。我已经更新了seo实现,以便能够为多个类别使用相同的关键字,请参阅:Opencart duplicate URL keywords 但这仅适用于类别。对于产品,我认为我只需要一个htaccess重写规则。
原始网址如下所示:
http://domain.com/index.php?route=product/product&path=25_28_93&product_id=1759
我的网址目前看起来像这样:
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。
页面变量表示分页,用于类别中的产品列表。此列表可以具有可变长度,因为它计算一个类别中的产品数量以及他应该在一页上显示的数量。
因此,如果原始网址为&page=2
且网址为route=product/category
,则应该创建一个这样的网址:http://domain.com/Tire-Retreading/Equalizing/2
答案 0 :(得分:2)
我仍然觉得添加的解释令人困惑,听起来我觉得你不太清楚URL重写是如何工作的,所以我会尝试解释一些URL重写基础。
使用网址重写可以做的是翻译&#39;漂亮&#39;网址细分(向用户显示的网址)进入 实际,长期&#39;非漂亮&#39;网址(使用变量来提供特定内容)。你可以通过使用正则表达式来匹配漂亮的段,然后给你的php变量提供预定义的值或那些匹配的值。
因此,您首先要做的是弄清楚您的漂亮网址应该是什么样的,以及它们各自的细分如何转化为您的变量。 (虽然您当然也可以使用任意数量的新变量,然后可以将其值转换并分配给PHP脚本中已存在或预定义的变量。)
你举了这个例子: http://example.com/Tire-Retreading/Equalizing/2
此网址似乎由您要转换为变量的三个部分组成:
Tire-Retreading
(变量route
)Equalizing
(变量path
)2
(变量page
)您必须根据拼写各个细分的所有可能方式构建您的RegEx以匹配您的细分,包括允许的任何特殊字符等。完成后,您可以将匹配的值传递给使用反向引用的变量(或者您可以定义自己的值以供变量使用)。
使用反向引用,你可以使用它:
RewriteRule ^([A-Za-z-]+)/([A-Za-z-]+)/([0-9]+)$ index.php?route=$1&path=$2&page=$3 [NC,L]
您必须将用于段匹配的单独RegEx(如[A-Za-z-]+
)放在圆括号中,以便能够以$1
的形式将结果分配给您的php变量, $2
等。
取决于是否还应允许用户浏览概述&#39;您的产品/类别或路径的页面,您可能必须从尽可能小的&#39;漂亮的&#39;开始重写。最长的URL。
E.g。
RewriteRule ^([A-Za-z-]+)$ $1/ [NC,R]
RewriteRule ^([A-Za-z-]+)/$ index.php?route=$1 [NC,L]
RewriteRule ^([A-Za-z-]+)/([A-Za-z-]+)$ $1/$2/ [NC,R]
RewriteRule ^([A-Za-z-]+)/([A-Za-z-]+)/$ index.php?route=$1&path=$2 [NC,L]
RewriteRule ^([A-Za-z-]+)/([A-Za-z-]+)/([0-9]+)$ $1/$2/$3/ [NC,R]
RewriteRule ^([A-Za-z-]+)/([A-Za-z-]+)/([0-9]+)/$ index.php?route=$1&path=$2&page=$3 [NC,L]
重写域名本身就在所有“路径”之前。重写。
您还必须弄清楚哪些标志 - 最后方括号中的部分 - 您需要使用,这意味着您必须更详细地阅读URL rewriting。在上面的示例中,用户键入的所有不以斜杠结尾的URL会自动重定向到(即方括号中的R
代表的)到同一个URL但添加了斜杠。因此,http://example.com/blah
会重定向到http://example.com/blah/
等。
根据您提供的信息,您将拥有以两个可能细分中的一个开头的网址:
http://example.com/In-Stock/...
VS
http://example.com/Product-or-Category/....
所以,你必须要特别小心,这两个人不要混淆。如果In-Stock
被汇集到自己的变量中并且总是拼写完全相同,那么您可能希望让您的第一个规则处理它并使用RegEx仅在以后匹配产品/类别(全部重写)规则是按时间顺序,一个接一个地处理的。)
我希望有所帮助。