用于Web应用程序的漂亮URL [英] Pretty URL's for web application

查看:64
本文介绍了用于Web应用程序的漂亮URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有100篇有关Mod Rewrite的文章,教程和手册,但是我倾向于通过示例来学习,所以我要问的是快速进行漂亮的url转换,我会继续前进(这样我就可以确切地了解它在做什么,以便可以将其应用于其他人)

I'm aware that there are a hundred articles, tutorials and manuals on Mod Rewrite but i tend to learn better by examples, so all i'm asking is for a quick pretty url conversion and i'll be on my way (so i can dissect exactly what its doing, so i can apply it to others)

我有以下URL: http://www.example.com/single.php ?id = 1

现在,如果它遵循与Stack Overflow相同的约定(其中id = 1是页面的实际标题),那就太好了.

Now it would be great if it could follow the same convention as Stack Overflow where the id=1 is the actual title of the page.

如果有帮助,我在PHP变量中有标题吗?

I have the title in a PHP variable if thats any help?

如果有人可以将上述内容转换为以下内容,我将不胜感激: http: //www.example.com/single/here-is-my-cool-title (最后一位是动态的,取决于PHP变量)

I would be grateful if someone could turn the above into: http://www.example.com/single/here-is-my-cool-title (where the last bit is dynamic, depending on a PHP variable)

有可能吗?

推荐答案

SO实际上在URL中具有帖子ID和标题,但它们仅使用帖子ID.标题不太适合,因为可能有重复的帖子(相同的标题,不同的ID),标题更改等.对于此问题的网址:

SO actually has the post ID and the title in the url, but they'll only use the post ID. The title isn't very suitable, as there may be duplicate posts (same title, different id), title changes, etc. For the url of this question:

/questions/5142095/pretty-urls-for-web-application

解决此问题:

/single.php?id=5142095

重写规则将是:

RewriteRule ^questions/([0-9]*)/(.*)$ /single.php?id=$1

寻找的是uri的开头(在域之后)^,单词questions,任意长度的数字[[0

What this looks for is the start of the uri (after the domain) ^, the word questions, any length of numbers `[0

这寻找:

  • ^ URI的开头
  • 单词questions
  • 斜线/
  • 可变长度的数字[0-9]*(例如1、123、1234等)
  • 斜线/
  • 任何字符.*的任何长度(即问题标题)
  • $ URI的结尾
  • ^ the start of the URI
  • the word questions
  • a slash /
  • a variable length of numbers [0-9]* (such as 1, 123, 1234 etc)
  • a slash /
  • any length of any character .* (ie the question title)
  • $ the end of the URI

当字符匹配项(例如[0-9] *或.*)放在方括号中时,mod_rewrite会将匹配结果放入带编号的Dollar变量中.在上面的规则中,[0-9]*匹配任何数字(和任何长度的数字)并将其放入$1.第二个匹配项.*匹配任何字符的任何长度,并将匹配项放入$2.

When the character match (such as [0-9]* or .*) is in brackets, mod_rewrite puts the match result into a numbered dollar variable. In the rule above, [0-9]* matches any number (and any length of numbers) and puts it into $1. The second match .* matches any length of any character, and puts the match into $2.

您的应用程序将在规则的右侧收到URI,其中美元变量将替换为匹配项.

Your application will receive the URI on the right side of the rule, with the dollar variables replaced with the matches.

这篇关于用于Web应用程序的漂亮URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆