谈中伪静态的实现-以opencart为例
具体有两种办法:
----------------------------------------------------------------------------------------------------------------------------------------
1.正则,.htaccess文件。不用查表,写正则,但静态的url也要符合一定的规律。
----------------------------------------------------------------------------------------------------------------------------------------
2.用.htaccess文件带参数类似于opencart中的_route_,定向到框架入口文件index.php,在框架控制器中查表,一个_route_值对应一个真实的url地址。可以随心所欲的定义静态的url,然后查表得到真实的url值。
.htaccess:
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
<?php class ControllerCommonSeoUrl extends Controller { public function index() { // 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); } foreach ($parts as $part) {//根据_route_值查表 $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($part) . "'"); 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') { if (!isset($this->request->get['path'])) { $this->request->get['path'] = $url[1]; } else { $this->request->get['path'] .= '_' . $url[1]; } } if ($url[0] == 'manufacturer_id') { $this->request->get['manufacturer_id'] = $url[1]; } if ($url[0] == 'information_id') { $this->request->get['information_id'] = $url[1]; } } 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';//如果有product_id值,则设定route } 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']); //根据<span style="font-family: Arial, Helvetica, sans-serif;">$this->request->get['route']</span>调用对应控制器的对应方法 } } } 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) { $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) { $url .= '/' . $query->row['keyword']; } else { $url = ''; break; } } unset($data[$key]); } elseif ($key == 'route' && $value == 'common/home') { $url = '/'; } } } if ($url) { unset($data['route']); $query = ''; if ($data) { foreach ($data as $key => $value) { $query .= '&' . rawurlencode((string)$key) . '=' . rawurlencode((string)$value); } if ($query) { $query = '?' . 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; } } }
对应数据表:
oc_url_alias
完成了以下url转换:
http://www.opencart_v2000_test.com/index.php?route=product/product&product_id=30;
http://www.opencart_v2000_test.com/canon-eos-5d-lifeifei;
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------