Web 应用程序的漂亮 URL
我知道有一百篇关于 Mod Rewrite 的文章、教程和手册,但我倾向于通过示例学习得更好,所以我所要求的只是快速漂亮的 url 转换,我会继续前进(所以我可以准确剖析它的作用,这样我就可以将其应用于其他人)
我有这个 URL: http ://www.example.com/single.php?id=1
现在,如果它能够遵循与 Stack Overflow 相同的约定(其中 id=1 是页面的实际标题),那就太好了。
我在 PHP 变量中有标题,这有什么帮助吗?
如果有人可以将上述内容转换为: http://www .example.com/single/here-is-my-cool-title(其中最后一位是动态的,取决于 PHP 变量)
这可能吗?
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)
I have this URL: http://www.example.com/single.php?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.
I have the title in a PHP variable if thats any help?
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)
Is that possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
SO 实际上在 url 中有帖子 ID 和标题,但他们只会使用帖子 ID。标题不是很合适,因为可能有重复的帖子(相同的标题,不同的id),标题更改等。对于这个问题的url:
要解决这个问题:
重写规则将是:
这个寻找的是uri 的开头(在域之后)
^
、单词questions
、任意长度的数字 `[0这将查找:
^
URI 的开头questions
/
[0-9]*
(例如 1、123、1234等)/
.*
(即问题标题)$
URI 结尾当字符匹配时 (例如[0-9]*或.*)在括号中,mod_rewrite将匹配结果放入一个编号的美元变量中。在上面的规则中,
[0-9]*
匹配任何数字(以及任意长度的数字)并将其放入$1
中。第二个匹配.*
匹配任何字符的任意长度,并将匹配项放入$2
中。您的应用程序将收到规则右侧的 URI,其中美元变量替换为匹配项。
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:
To resolve to this:
The rewrite rule will be:
What this looks for is the start of the uri (after the domain)
^
, the wordquestions
, any length of numbers `[0This looks for:
^
the start of the URIquestions
/
[0-9]*
(such as 1, 123, 1234 etc)/
.*
(ie the question title)$
the end of the URIWhen 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
.Your application will receive the URI on the right side of the rule, with the dollar variables replaced with the matches.
你对 mod rewrite 的文章数量是正确的 - 就我个人而言,我不喜欢一直添加额外的规则,然后担心该规则是否会被捕获......
为此,我更喜欢简单地传递很多内容到根索引文件,然后让服务器端决定如何处理它。
看看这个。
You are right about the number of articles on mod rewrite - personally I don't like having to add extra rules all the time and then worry if that rule is going to be caught...
To that end I prefer simply passing the lot through to the root index file and then allow server side to decide what to do with it.
Have a look at this.
与其他答案不同,这个答案将为您提供整个问题的解决方案,而不是特定的问题。因为它是基于现实生活的经验,而不是纯粹的理论。
此答案包含您需要的一切,包括旧式的 SEO 友好重定向用于更改旧脚本以使它们照常运行的网址和代码。
您唯一要做的就是更改网站上的链接生成代码。我希望没什么大不了的。
Unlike others, this answer will give you solution not for the particular question but for the whole problem. Just because it's based on real life experience, not bare theory.
This answer contains everything you need, including SEO-friendly redirect from old-style urls and code for altering old scripts to make them run as usual.
The only thing left for you to do is to alter links generation code on your site. Not a big deal I hope.
您必须使用 Apache 的 mod-rewrite,这是一个示例:
编辑:最好在重写中添加整数标识符,例如:
You have to use Apache's mod-rewrite, here's an example:
Edit: it's a good idea to add integer identifier to your rewrite, like: