.htaccess中的mod_rewrite

时间:2010-06-20 10:36:24

标签: php .htaccess mod-rewrite blogs

嘿所有,我怎么能减少这个:

www.example.com/index.php?page=viewblog&category=awesome

到此为止:

www.example.com/blog/awesome

以上列出了该类别中的所有博客文章,但我也希望在其末尾添加帖子标题的范围,如下所示:

www.example.com/index.php?page=viewblog&category=awesome&post=why-trees-are-green

这需要缩短为:

www.example.com/blog/awesome/why-trees-are-green

任何想法,任何人?在此先感谢:)

2 个答案:

答案 0 :(得分:2)

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f    
RewriteRule blog/([^/]+)/?$ index.php?page=viewblog&category=$1 [L]
RewriteRule blog/([^/]+)/([^/]+)/?$ index.php?page=viewblog&category=$1&post=$2 [L]

条件确保请求的URL不是您要提供的实际文件(css,图像等)

然后,每个级别一个规则,第一个将重写 http://yourserver.com/blog/whateverhttp://yourserver.com/index.php?page=viewblog&category=whatever

第二个将重写http://yourserver.com/blog/whatever/whenever http://yourserver.com/index.php?page=viewblog&category=whatever&post=whenever

如果您需要更多关卡,请相应添加更多规则。

答案 1 :(得分:0)

这样的东西?

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^./]+)/([^./]+)$ index.php?page=view$1&category=$2 [L]
RewriteRule ^([^./]+)/([^./]+)/([^./]+)$ index.php?page=view$1&category=$2&post=$3 [L]