0

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

假设我有一个数字下载商店,我希望我的 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 讨厌我,我希望有人可以帮助创建一个漂亮的代码行来帮助我和其他好奇的人这样做。

谢谢

4

1 回答 1

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)
于 2013-05-29T15:53:37.650 回答