【问题标题】:Creating static, SEO friendly URLs with mod-rewrite使用 mod-rewrite 创建静态、SEO 友好的 URL
【发布时间】:2013-05-29 14:16:22
【问题描述】:

好的,我已经修改了之前的问题,以使我的意图更加明确,并希望能帮助其他希望做类似事情的人。

假设我有一个数字下载商店,我希望我的 URL 看起来像这样:

downloads.com/music/this-song-name/3873         // A url to a unique track
downloads.com/music/featured-stuff      // A url that links to unique content
downloads.com/music                 // The homepage for that section

我也可以有这样的网址

downloads.com/videos/this-video/3876

等等

现在,服务器端,PHP 完成了所有工作。它需要 URL:

downloads.com/?a=music&b=this-song-name&c=37863 // load page a, get ID c from database 

 downloads.com/?a=music     // Just load the default music page

我需要 .htaccess 来改变

url.com/?a=1&b=2&c=3` 

 url.com/1/2/3

a、b 和 c 是固定的,将是用于解析数据的唯一参数(例如,您不会很快找到 ?f=music)

我遇到的一个问题是,如果所有三个参数都存在,我可以让它工作,但如果拿走一个参数,它就不会工作。

我不是 REGEX 专家,并且 mod re-write 讨厌我,我希望那里有人可以帮助创建一个可以帮助我的漂亮代码行 和其他好奇这样做的人。

谢谢

【问题讨论】:

    标签: regex .htaccess url mod-rewrite seo


    【解决方案1】:

    我认为您已经掌握了基本的 mod 重写规则。

    你要找的代码是这样的:

    RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ index.php?a=$1&b=$2&c=$3
    RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ index.php?a=$1&b=$2
    RewriteRule ^([a-zA-Z0-9_-]+)/?$ index.php?a=$1
    

    正则表达式解释

    ^                # Matches the start of the string (the URL)
    (                # Indicates the start of a capturing group (in this case the thing we want to replace)
    [a-zA-Z0-9_-]+   # Matches 1 or more letters, numbers, underscore's and -'s (+ = 1 or more * = 0 or more) 
    )                # Indicates the end of the capturing group
    /?               # Matches 0 or 1 times the '/' symbol 
    $                # Matches the end of the string (the URL)
    

    【讨论】:

    • 太棒了!你不知道这有多大帮助!谢谢
    • @BillyJakeO'Connor:那么别忘了接受这个答案。
    猜你喜欢
    • 2017-11-08
    • 2018-09-20
    • 1970-01-01
    • 2011-09-23
    • 2015-12-10
    • 2019-04-30
    • 2015-01-04
    • 2014-12-04
    • 2011-06-11
    相关资源
    最近更新 更多