Bak*_*ap4 9 php url .htaccess mod-rewrite opencart
我想要从我的opencart网上商店重写我的产品网址.Opencart本身有一个seo实现,真的很糟糕.我已经更新了seo实现,以便能够为多个类别使用相同的关键字,请参阅:Opencart重复URL关键字 但这仅适用于类别.对于产品,我认为我只需要一个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]
Run Code Online (Sandbox Code Playgroud)
我的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;
}
}
Run Code Online (Sandbox Code Playgroud)
分页代码是这样的:
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 '';
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:
我的所有页面都被重定向到http://domain.com/index.php.
从那里它决定了route参数中要使用的目录/文件.所以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
我仍然发现添加的解释令人困惑,而且在我看来,您不太了解 URL 重写的工作原理,因此我将尝试解释一些 URL 重写基础知识。
\n\n通过 URL 重写,您可以将“漂亮”的 URL段(向用户显示的 URL)转换为实际的、长的、“不漂亮”的 URL段(使用变量来提供某些内容) 。您可以通过使用正则表达式来匹配漂亮的段,然后为您的 php 变量提供预定义值或那些匹配的值。
\n\n因此,您首先要做的是弄清楚漂亮的 URL 应该是什么样子,以及它们的各个部分如何转换为您的变量。(当然,您也可以使用任意数量的新变量,然后您可以将其值转换并分配给 php 脚本中已有的或预定义的变量。)
\n\n您给出了以下示例:\n http://example.com/Tire-Retreading/Equalizing/2
\n\n该 URL 似乎由您想要转换为变量的三个部分组成:
\n\nTire-Retreading(多变的route)Equalizing(多变的path)2(多变的page)您必须根据各个段的所有可能拼写方式(包括允许的任何特殊字符等)构建正则表达式来匹配您的段。完成后,您可以使用反向引用(或者您可以定义自己的值以供变量使用)。
\n\n使用反向引用,你可以使用这个:
\n\nRewriteRule ^([A-Za-z-]+)/([A-Za-z-]+)/([0-9]+)$ index.php?route=$1&path=$2&page=$3 [NC,L]\nRun Code Online (Sandbox Code Playgroud)\n\n您必须将分段匹配的各个正则表达式(例如[A-Za-z-]+)放在圆括号中,以便能够以 等形式将结果分配给您的 php变量$1。$2
根据是否允许用户浏览您的产品/类别或路径的“概述”页面,您可能必须从尽可能小的“漂亮”URL 开始重写到最长的 URL。
\n\n例如
\n\nRewriteRule ^([A-Za-z-]+)$ $1/ [NC,R]\nRewriteRule ^([A-Za-z-]+)/$ index.php?route=$1 [NC,L]\nRewriteRule ^([A-Za-z-]+)/([A-Za-z-]+)$ $1/$2/ [NC,R]\nRewriteRule ^([A-Za-z-]+)/([A-Za-z-]+)/$ index.php?route=$1&path=$2 [NC,L]\nRewriteRule ^([A-Za-z-]+)/([A-Za-z-]+)/([0-9]+)$ $1/$2/$3/ [NC,R]\nRewriteRule ^([A-Za-z-]+)/([A-Za-z-]+)/([0-9]+)/$ index.php?route=$1&path=$2&page=$3 [NC,L]\nRun Code Online (Sandbox Code Playgroud)\n\n域本身的重写在所有“路径”重写之前进行。
\n\n您还必须弄清楚需要使用哪些标志 \xe2\x80\x93 末尾的方括号中的部分 \xe2\x80\x93 ,这意味着您必须阅读更多有关 URL 重写的内容细节。在上面的示例中,用户键入的所有不以斜杠结尾的 URL 都会自动重定向(这就是方R括号中所代表的内容)到相同的 URL,但添加了斜杠。所以,http://example.com/blah会重定向到http://example.com/blah/等。
根据您提供的信息,您将拥有以两个可能的段之一开头的网址:
\n\nhttp://example.com/In-Stock/...\nRun Code Online (Sandbox Code Playgroud)\n\n与
\n\nhttp://example.com/Product-or-Category/....\nRun Code Online (Sandbox Code Playgroud)\n\n因此,您必须格外小心,不要混淆这两者。如果In-Stock被汇集到它自己的变量中并且总是拼写完全一样,您可能希望让您的第一个规则处理该问题,并仅在稍后使用正则表达式来匹配产品/类别(所有重写规则均按时间顺序处理,一个接一个地)。
我希望这有帮助。
\n