重写网址搜索结果和分页

时间:2013-03-16 02:51:15

标签: php mod-rewrite url-rewriting

大家好我试着用mod重写做一个搜索结果网址

我的网址是这个

products/?categorie=2&subcategorie=&make=&p=2  (P is Page)

我试过这个

RewriteRule ^products([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /? categorie=$1&subcategorie=$2&make=$3&p=$4 [L]

这个分页不起作用

我的htaccess看起来很

RewriteEngine On
# =========== Configuracoes Default =========== #
AddDefaultCharset UTF-8
RewriteCond %{REQUEST_FILENAME} !^(.+)\.(css|js|xml|swf|pdf|doc|rtf)$
RewriteCond %{REQUEST_FILENAME} !^(.+)\.(jpg|png|gif)$
RewriteCond %{REQUEST_FILENAME} !^(.+)\.(ttf|otf|woff|eot|svg)$
RewriteCond %{REQUEST_FILENAME} !/php/acoes.php
RewriteCond %{REQUEST_FILENAME} !favicon.ico
RewriteCond %{REQUEST_FILENAME} !apple-touch-icon.png
RewriteCond %{REQUEST_FILENAME} !robots.txt

RewriteRule ^(.+)\/?([0-9]*)\/?$ php/index.php
#RewriteRule ^products([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /? categorie=$1&subcategorie=$2&make=$3&p=$4 [L]

我的php文件位于文件夹名称php

我的php文件返回搜索结果是这个

<?php
$Subcategorie_Id = mysql_real_escape_string($_REQUEST["subcategorie"]);
$Categorie_Id = mysql_real_escape_string($_REQUEST["categorie"]);
$Make_Id = mysql_real_escape_string($_REQUEST["make"]);

$SqlCount = "SELECT Product_Id FROM tabproducts WHERE Product_Status =1";
if($Categorie_Id) $SqlCount .= " AND tabproducts.Categorie_Id = '$Categorie_Id' ";
if($Subcategorie_Id) $SqlCount .= " AND tabproducts.Subcategorie_Id = '$Subcategorie_Id' ";
if($Make_Id) $SqlCount .= " AND tabproduct.Make_Id = '$Make_Id' ";
$QueryCount = mysql_query($SqlCount, $Conn) or die(mysql_error($Conn));
$RsCount = mysql_fetch_array($QueryCount);
$recordCount = mysql_num_rows($QueryCount);
if (!$_REQUEST["p"]) $p = 1;
else $p = $_REQUEST["p"];
$pageSize = 24;
$itemIni = ($pageSize*$p)-$pageSize;
$totalPage = ceil($recordCount/$pageSize);  
?>

1 个答案:

答案 0 :(得分:0)

查询位于变量%{QUERY_STRING}中,而不是URI路径中。如果传入的URL是这个:

http://example.com/products/?categorie=2&subcategorie=56&make=3&p=2

例如,根据您的问题,规则集应该是这样的:

# Check it is the correct query
RewriteCond %{QUERY_STRING}  ^categorie=[^&]*&subcategorie=[^&]*&make=[^&]*&p=[^&]*  [NC]

# Prevent loops
RewriteCond %{REQUEST_URI}  ^/products/?    [NC]

# Map silently 
RewriteRule  .*  /   [L]

无声地映射

http://example.com/products/?categorie=2&subcategorie=56&make=3&p=2

http://example.com/?categorie=2&subcategorie=56&make=3&p=2

无需将查询附加到替换网址,因为它会自动添加。

对于永久和可见的重定向,将[L]替换为[L,R = 301]。

注意:

我认为应该放置以上几行:

RewriteEngine On
# HERE **********
# =========== Configuracoes Default =========== #
...

在你问题中的一套规则中。