mod_rewrite 和 Pretty url 背后的概念是什么?
我用谷歌搜索了很多,阅读了很多,但仍然无法弄清楚漂亮的 url 和 mod_rewrite 背后的基本概念。
我目前正在以 php 方式制作漂亮的 url。像这样
a)在表中创建一个字段,其名称与文本(或我想要在url中的标题)相同,并用(-)分隔。
b) 使用相同的字段值创建超链接,例如 (a href="my-page-name")
c) 当通过查询字符串传递时,检查数据库中的值,如果匹配,则获取该行的 id 并执行其他操作...
到目前为止它正在工作..但我很困惑。
这是正确的方法吗?或者它可以通过 .htaccess 进行控制,因为我在所有需要漂亮 URL 的页面中都做了同样的事情。
请有人指导我..
感谢您的阅读;)
I have googled a lot, read a lot but still cannot figure out the basic concept behind pretty url and mod_rewrite.
I am currently making pretty url the php way. Like this
a) creating a field in a table with the name same as the text (or the title which i want in the url) separated with (-).
b) making hyperlink with the same field value like (a href="my-page-name")
c) and when passed through querystring checking the value from the database and if it matches, get the id for that row and do other stuffs...
So far it is working.. but I am confused.
IS THIS THE RIGHT WAY ? OR it can be controlled through .htaccess, since I am doing the same thing in all the pages which needs pretty URL.
Someone guide me please..
Thanks for reading ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实现由你决定,.htaccess 将包含匹配请求的 URL 的一部分并将其传递给你的脚本的规则,然后由你决定做你想做的事情,通常在使用 url 重写时保留 id,但他们添加了标题的“干净”版本,例如:
.htaccess 将匹配 id (
1
) 和干净的标题 (my-first-post
),并且将它们传递到您的页面,该页面反过来应该获取参数,获取具有该 id 的页面并显示它。通过这种方式,页面可以利用漂亮的 URL,同时又不会失去快速的 ID 查找功能。注意:
http://example.com/blog/1-wrong-title
将重定向到http://example.com/blog/1-my-first-post
>。id
,并依赖缓存的clean-title表字段来查找文章,你当然应该考虑对该字段进行唯一索引对于快速查找,这种方法的一个例子是维基百科(Mediawiki)。The implementation is up to you, .htaccess would contain rules that would match a part of the requested URL and pas it to your script, and then it's up to you to do what you want, usually when using url rewriting the id is retained, but they add the "clean" version of the title to it, like:
the .htaccess will match both id (
1
) and clean title (my-first-post
), and pass them to your page, which in turn should grab the parameters get the page with that id and display it. This way page uses takes advantage of pretty URLs without losing the quick id lookup.Notes:
http://example.com/blog/1-wrong-title
would redirect tohttp://example.com/blog/1-my-first-post
.id
in the url, and depend on the cached clean-title table field to lookup the article, you should of course consider uniquely-indexing that field for fast lookups, an example of that approach is Wikipedia's (Mediawiki).