【问题标题】:Writing .htaccess mod rewrite for hierarchical categories为分层类别编写 .htaccess mod rewrite
【发布时间】:2010-05-13 02:22:09
【问题描述】:
我需要为我的分类广告目录重写网址
我有 4 种类型的链接
/City ==> 显示城市中的所有广告
/City/Cat1 ==> 显示城市+类别中的所有广告
/City/Cat1/Cat2 ==>在城市+类别1+类别2中展示添加广告
/City/Cat1/Cat2/Ad-id ==> 显示广告本身并传递 cat1 cat2 和 city 变量
原始隐藏网址应为 index.php?city=alexandria&cat1=cars&cat2=bikes&adid=EWSw22d
你能帮我为这个结构编写.htaccess
【问题讨论】:
标签:
php
.htaccess
mod-rewrite
directory
【解决方案1】:
您可能应该将第一个重写规则替换为 rewritecond,但想法是这样的:
RewriteEngine on
RewriteRule ^index.php index.php [NE,QSA,L]
RewriteRule ^(.*?)(?:/(.*?)(?:/(.*?)(?:/(.*))?)?)?$ index.php?city=$1&cat1=$2&cat2=$3&adid=$4
【解决方案2】:
我会使用单一规则将所有不存在的请求还原到索引 PHP,然后在那里解析请求字符串。像这样的东西
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?query=$1 [QSA,L]