如何通过'RewriteCond'简化.htaccess Rewriterule?

时间:2013-04-15 15:57:32

标签: .htaccess mod-rewrite

我的.htaccess看起来像这样:

    #01
RewriteRule ^([a-z]+)/yule/*$ index.php?city=$1&category=2

RewriteRule ^([a-z]+)/yule/today/*$ index.php?city=$1&category=2&today=true
RewriteRule ^([a-z]+)/yule/today/(price|rebate|bought)_(desc|asc)/ index.php?city=$1&category=2&today=true&order=$2&ordertype=$3
RewriteRule ^([a-z]+)/yule/(price|rebate|bought)_(desc|asc)/ index.php?city=$1&category=2&order=$2&ordertype=$3

RewriteRule ^([a-z]+)/yule_([0-9]+)-([0-9]+)?/*$ index.php?city=$1&category=2&price_sta=$2&price_end=$3
RewriteRule ^([a-z]+)/yule_([0-9]+)-([0-9]+)?/today/*$ index.php?city=$1&category=2&price_sta=$2&price_end=$3&today=true
RewriteRule ^([a-z]+)/yule_([0-9]+)-([0-9]+)?/today/(price|rebate|bought)_(desc|asc)/ index.php?city=$1&category=2&price_sta=$2&price_end=$3&today=true&order=$4&ordertype=$5
RewriteRule ^([a-z]+)/yule_([0-9]+)-([0-9]+)?/(price|rebate|bought)_(desc|asc)/ index.php?city=$1&category=2&price_sta=$2&price_end=$3&order=$4&ordertype=$5

#02
RewriteRule ^([a-z]+)/travel/*$ index.php?city=$1&category=3

RewriteRule ^([a-z]+)/travel/today/*$ index.php?city=$1&category=3&today=true
RewriteRule ^([a-z]+)/travel/today/(price|rebate|bought)_(desc|asc)/ index.php?city=$1&category=3&today=true&order=$2&ordertype=$3
RewriteRule ^([a-z]+)/travel/(price|rebate|bought)_(desc|asc)/ index.php?city=$1&category=3&order=$2&ordertype=$3

RewriteRule ^([a-z]+)/travel_([0-9]+)-([0-9]+)?/*$ index.php?city=$1&category=3&price_sta=$2&price_end=$3
RewriteRule ^([a-z]+)/travel_([0-9]+)-([0-9]+)?/today/*$ index.php?city=$1&category=3&price_sta=$2&price_end=$3&today=true
RewriteRule ^([a-z]+)/travel_([0-9]+)-([0-9]+)?/today/(price|rebate|bought)_(desc|asc)/ index.php?city=$1&category=3&price_sta=$2&price_end=$3&today=true&order=$4&ordertype=$5
RewriteRule ^([a-z]+)/travel_([0-9]+)-([0-9]+)?/(price|rebate|bought)_(desc|asc)/ index.php?city=$1&category=3&price_sta=$2&price_end=$3&order=$4&ordertype=$5

#03
RewriteRule ^([a-z]+)/service/*$ index.php?city=$1&category=4

RewriteRule ^([a-z]+)/service/today/*$ index.php?city=$1&category=4&today=true
RewriteRule ^([a-z]+)/service/today/(price|rebate|bought)_(desc|asc)/ index.php?city=$1&category=4&today=true&order=$2&ordertype=$3
RewriteRule ^([a-z]+)/service/(price|rebate|bought)_(desc|asc)/ index.php?city=$1&category=4&order=$2&ordertype=$3

RewriteRule ^([a-z]+)/service_([0-9]+)-([0-9]+)?/*$ index.php?city=$1&category=4&price_sta=$2&price_end=$3
RewriteRule ^([a-z]+)/service_([0-9]+)-([0-9]+)?/today/*$ index.php?city=$1&category=4&price_sta=$2&price_end=$3&today=true
RewriteRule ^([a-z]+)/service_([0-9]+)-([0-9]+)?/today/(price|rebate|bought)_(desc|asc)/ index.php?city=$1&category=4&price_sta=$2&price_end=$3&today=true&order=$4&ordertype=$5
RewriteRule ^([a-z]+)/service_([0-9]+)-([0-9]+)?/(price|rebate|bought)_(desc|asc)/ index.php?city=$1&category=4&price_sta=$2&price_end=$3&order=$4&ordertype=$5

#04
RewriteRule ^([a-z]+)/gou/*$ index.php?city=$1&category=5

RewriteRule ^([a-z]+)/gou/today/*$ index.php?city=$1&category=5&today=true
RewriteRule ^([a-z]+)/gou/today/(price|rebate|bought)_(desc|asc)/ index.php?city=$1&category=5&today=true&order=$2&ordertype=$3
RewriteRule ^([a-z]+)/gou/(price|rebate|bought)_(desc|asc)/ index.php?city=$1&category=5&order=$2&ordertype=$3

RewriteRule ^([a-z]+)/gou_([0-9]+)-([0-9]+)?/*$ index.php?city=$1&category=5&price_sta=$2&price_end=$3
RewriteRule ^([a-z]+)/gou_([0-9]+)-([0-9]+)?/today/*$ index.php?city=$1&category=5&price_sta=$2&price_end=$3&today=true
RewriteRule ^([a-z]+)/gou_([0-9]+)-([0-9]+)?/today/(price|rebate|bought)_(desc|asc)/ index.php?city=$1&category=5&price_sta=$2&price_end=$3&today=true&order=$4&ordertype=$5
RewriteRule ^([a-z]+)/gou_([0-9]+)-([0-9]+)?/(price|rebate|bought)_(desc|asc)/ index.php?city=$1&category=5&price_sta=$2&price_end=$3&order=$4&ordertype=$5

#05
RewriteRule ^([a-z]+)/qita/*$ index.php?city=$1&category=6

RewriteRule ^([a-z]+)/qita/today/*$ index.php?city=$1&category=6&today=true
RewriteRule ^([a-z]+)/qita/today/(price|rebate|bought)_(desc|asc)/ index.php?city=$1&category=6&today=true&order=$2&ordertype=$3
RewriteRule ^([a-z]+)/qita/(price|rebate|bought)_(desc|asc)/ index.php?city=$1&category=6&order=$2&ordertype=$3

RewriteRule ^([a-z]+)/qita_([0-9]+)-([0-9]+)?/*$ index.php?city=$1&category=6&price_sta=$2&price_end=$3
RewriteRule ^([a-z]+)/qita_([0-9]+)-([0-9]+)?/today/*$ index.php?city=$1&category=6&price_sta=$2&price_end=$3&today=true
RewriteRule ^([a-z]+)/qita_([0-9]+)-([0-9]+)?/today/(price|rebate|bought)_(desc|asc)/ index.php?city=$1&category=6&price_sta=$2&price_end=$3&today=true&order=$4&ordertype=$5
RewriteRule ^([a-z]+)/qita_([0-9]+)-([0-9]+)?/(price|rebate|bought)_(desc|asc)/ index.php?city=$1&category=6&price_sta=$2&price_end=$3&order=$4&ordertype=$5

#06
#07
#08
... ...

我有很多类别,例如:'yule','travel','service','gou','qita',~~~~很多类别~~~~~~这些类别对应于这样的id :'meishi' - > category = 1'yule' - > category = 2'travel' - > category = 3'service' - > category = 4'gou' - > category = 5'qita' - > category = 6 ~~ ~~~~~如何简化这些类别使用​​'RewriteCond'?

1 个答案:

答案 0 :(得分:1)

查看有关重写映射的文档:http://httpd.apache.org/docs/current/rewrite/rewritemap.html

您的目标是通过文本文件将人类可读类别slug映射到类别编号。

例如,如果您输入/travel作为您的网址,则映射器会查阅该文本文件并返回3类别

因此,([a-z]+)/([a-z]+)将成为index.php?city=$1&category=${examplemap:$1|/404.php}。 例如,mycity/travel在查找/地图之后将变为index.php?city=mycity&category=3

然后,使用重写规则,您不需要为每个类别重复所有这些规则。

如果您对文档有困难,请告诉我,我会尝试为您整理一些内容。