创建静态的,搜索引擎优化与mod-rewrite友好的URL静态、友好、搜索引擎优化、mod

2023-09-02 20:40:26 作者:故事与旧友

好吧,我已经修改了previous的问题,使我的意图更加清晰,希望能帮助别人希望做类似的事情。

Okay I have revised a previous question to make my intentions more clear and hopefully to help others wishing to do similar things.

让我们说我有一个数字下载商店,我希望我的URL看起来是这样的:

Let's say I have a digital download store, I want my URL's to look like this:

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

我也能有这样的网址

I could also have a url like this

downloads.com/videos/this-video/3876

等。

现在,服务器端,PHP做所有的工作。它采用的网址:

Now, Server side, PHP does all the work. It takes the 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修改

I need .htaccess to change

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

 url.com/1/2/3

A,B和C是固定的,将是用于分析数据的唯一参数(例如,你不会发现?F =音乐很快)

a,b and c are fixed and will be the only parameters used to parse data (e.g you won't find ?f=music anytime soon)

有一个问题我已经是,我可以得到它的工作,如果所有三个参数是present,但它不会工作,如果一个人被带走。

One problem I've had is, I can get it to work if all three parameters are present but it will not work if one is taken away.

我不是正则表达式专家和mod重新写恨我,我希望有人在那里可以帮助创造code,将有助于一个美丽的线我 和其他人好奇地这样做。

I'm no REGEX expert and mod re-write hates me, I was hoping someone out there could help create a beautiful line of code that would aid me and others curious to do so.

感谢

推荐答案

我想你已经有了基本的mod-rewrite规则。

I figure that you already have the basic mod rewrite rules.

您正在寻找的code是这样的:

The code you are looking for is this:

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

正则表达式的解释

Regex explanation

^                # 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)