PHP:路由器框架,实际上走向“漂亮”网址
我已经实现了我的路由器框架,我基本上使用查找表来告诉 url 是什么。
我原来的网址有一些丑陋的东西,例如domain.com?id=26,现在将有:domain.com/words-that-look-nice
但我的问题是: 如果所有页面都将被查找并重定向到,则实际的 HTML 标记将位于 1 个名为 my_page.php 的文件中 - 正确吗?唯一的区别是 my_page.php 对用户的显示会有所不同,具体取决于正在查找的项目,对吗?
如果是这样,那么我该如何“屏蔽”网址?
我的意思是,一旦我知道 url 是什么,我如何使页面 my_page.php 提供所查找的特定页面的所有数据?
如果问题很复杂,谢谢并抱歉:)
I have implemented my router framework where I basically use a lookup table to tell what the url will be.
My original urls which had some ugly stuff like domain.com?id=26 for example, will now have: domain.com/words-that-look-nice
But my question is:
If all the pages will be looked up and redirected to, the actual HTML markup will be in 1 file called my_page.php - correct? The only difference will be that my_page.php will appear differently to the user depending on the item being looked up, correct?
If so, then how do I do this "masking" of the url ??
I mean, once I know what the url will be, how do I make the page my_page.php serve all the data for that particular page that is looked up?
Thanks and sorry if question is convoluted :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您首先需要做的是确定是否可以使用“重写”。如果您使用“mod_rewrite”运行 apache,那么您将能够使用它。
将以下内容放入其中:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
现在做什么会发生的情况是,所有
"domain.com/this-is-my-page"
形式的请求都被重写为 "domain.com/index.php?r=this-is- my-page"设置您的路由器以在数据库中查找 r 的值。你的表看起来像这样:
身份证 |网址
55 | 55这是我的页面
56 | 56 this-is-my-next-page
所以运行类似
“从路由器WHERE URL=LOWER('{$r}')中选择ID”
。当然,不要忘记检查 sql 注入攻击。您现在拥有了自己的 ID,并且可以像现在一样填充页面。
希望有帮助:)
What you need to do first is work out if you can use "rewrites". If you are running apache with "mod_rewrite" then you will be able to use it.
Put the following in there:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Now what will happen is that all requests in the form of
"domain.com/this-is-my-page"
are rewritten to the form "domain.com/index.php?r=this-is-my-page"Setup your router to lookup the value of r in the database. Your table would look like this:
ID | URL
55 | this-is-my-page
56 | this-is-my-next-page
so run something like
"SELECT ID FROM router WHERE URL=LOWER('{$r}')"
. ofcourse don't forget to check for sql injection attacks.You now have you ID and you can populate the page as you do at the moment.
Hope that helps :)