【问题标题】:mod_rewrite for friendly URL'smod_rewrite 用于友好的 URL
【发布时间】:2012-02-25 13:48:49
【问题描述】:

我正在尝试在我的内容管理系统中实现永久链接,但我似乎遇到了 mod_rewrite 的问题。我有一个 PHP 文件 (single.php),它将根据传递给它的永久链接名称显示单个帖子。即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

【问题讨论】:

    标签: apache .htaccess url mod-rewrite rewrite


    【解决方案1】:

    你应该使用这个代码:

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

    如果您不想要外部重定向(在浏览器中更改 URL),请移除 R 标志。

    【讨论】:

    • 非常感谢,这正是我正在寻找的答案 :)
    猜你喜欢
    • 1970-01-01
    • 2011-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    • 2023-03-12
    • 1970-01-01
    • 2011-06-11
    相关资源
    最近更新 更多