mod_rewrite用于友好的URL' s

时间:2012-02-02 21:27:07

标签: apache .htaccess url mod-rewrite rewrite

我正在尝试将永久链接应用到我的内容管理系统中,而且我似乎陷入了mod_rewrite的问题。我有一个PHP文件(single.php),它将根据传递给它的Permalink名称显示一个帖子。即post.php?permalink=name-of-post-here

以下是我设定的规则:

RewriteRule ^([0-9]{4})/([a-z]+)?$ $1/ [R]
RewriteRule ^([0-9]{4})/([a-z]+)?$ post.php?permalink=$1

另外,如何获取date/year(即2012),并将其指定为PHP文件的参数。 (即post.php?year=2012?permalink=name-of-post-here)&可以使用mod_rewrite完成所有这些吗?

非常感谢。

想要改变

http://www.website.com/post.php?year=2012&permalink=post-name-here

http://www.website.com/2012/post-name-here

1 个答案:

答案 0 :(得分:3)

您应该使用以下代码:

Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteRule ^([0-9]{4})/(.*)$ post.php?year=$1&permalink=$2 [L,R,NC,QSA]

如果您不想要外部重定向(在浏览器中更改网址),请删除R标记。