重写像这样的URL的条件

时间:2012-08-23 17:27:07

标签: apache .htaccess mod-rewrite

如何编写mod_rewrite的条件才能使这个例子发生?

有一个像这样的网址......

http://domain.com/recent/5

渲染此页......

http://domain.com/index.php?view=recent&page=5

如果没有指定页码(请注意我希望它可以使用或不使用尾部斜杠)...

http://domain.com/recent/

默认为第1页......

http://domain.com/index.php?view=recent&page=1

但是当有人进入根域时......

http://domain.com

我希望此页面呈现...

http://domain.com/index.php?view=popular&page=1

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

将它放在文档根目录中的htaccess文件中:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([0-9]+) /index.php?view=$1&page=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /index.php?view=$1 [L,QSA]
RewriteRule ^$ /index.php?view=popular&page=1 [L,QSA]
相关问题