【问题标题】:url friendly and keeping hierarchy网址友好并保持层次结构
【发布时间】:2013-10-22 10:56:17
【问题描述】:

我正在将我的 url 转换为友好的 url,我正在为其中的一些而苦苦挣扎,

例如,让我们说帖子和类别,

帖子(详细):

/post/the-title/10 /* /post.php?id=10 */

帖子(列表)

/posts/  /* /posts.php?what=last */
/posts/?what=top /* /posts.php?what=top */
...

但是我不太确定如何实现类别,为了保持 url 的结构,我想完成:

/posts/category-name/5 /* /posts.php?what=cat&id=5 */

但这就是我重写我的网址的方式(用于列出):

RewriteRule ^posts/$ posts.php?$1&friendly=1 [QSA]

所以我相信我应该预先添加它,这样另一个就不会触发,例如:

RewriteRule ^posts/(.+)/(.+) posts.php?what=cat&id=$2&friendly=1
RewriteRule ^posts/$ posts.php?$1&friendly=1 [QSA]

所以这里的问题是:

1) 这是一条(近)路吗? (预计 url 类型会改变)
2) 在另一个之前建立 RewriteRule 是否会确保它不会陷入循环?

欢迎任何输入,我想在提交 url 结构之前考虑一下

【问题讨论】:

    标签: php .htaccess mod-rewrite hierarchy friendly-url


    【解决方案1】:

    对于“/post/the-title/10 /* /post.php?id=10 */”

    RewriteRule ^post/[^/]+/([0-9]+)/?$ /post.php?id=$1&friendly=1 [L,QSA]
    

    对于“/posts/ /* /posts.php?what=last */”

    RewriteCond %{QUERY_STRING} !^what=
    RewriteRule ^posts/?$ posts.php?what=last&friendly=1 [L,QSA]
    

    对于“/posts/?what=top /* /posts.php?what=top */”

    RewriteCond %{QUERY_STRING} ^what=
    RewriteRule ^posts/?$ posts.php?friendly=1 [L,QSA]
    

    对于“/posts/category-name/5 /* /posts.php?what=cat&id=5 */”

    RewriteRule ^posts/[^/]+/([0-9]+)/?$ /posts.php?what=cat&id=$1&friendly=1 [L,QSA]
    

    【讨论】:

    • 感谢您的分析,所以顺序无关紧要?
    • @ToniMichelCaubet 上面的顺序很好,因为正则表达式模式之间没有重叠。
    • 好一个@Jon +1。我已经修复了查询字符串中缺少的}。我相信它可能会因错误而困扰 OP。
    猜你喜欢
    • 2013-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-15
    • 2016-03-11
    • 2011-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多