PHP:路由器框架,实际上走向“漂亮”网址

发布于 2024-10-16 15:39:16 字数 363 浏览 9 评论 0原文

我已经实现了我的路由器框架,我基本上使用查找表来告诉 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美男兮 2024-10-23 15:39:16

您首先需要做的是确定是否可以使用“重写”。如果您使用“mod_rewrite”运行 apache,那么您将能够使用它。

  1. 在根目录中创建一个 .htaccess 文件...domain.com/。
    将以下内容放入其中:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?r=$1 [L,QSA] 
  1. 现在做什么会发生的情况是,所有 "domain.com/this-is-my-page" 形式的请求都被重写为 "domain.com/index.php?r=this-is- my-page"

  2. 设置您的路由器以在数据库中查找 r 的值。你的表看起来像这样:
    身份证 |网址
    55 | 55这是我的页面
    56 | 56 this-is-my-next-page

  3. 所以运行类似“从路由器WHERE URL=LOWER('{$r}')中选择ID”。当然,不要忘记检查 sql 注入攻击。

  4. 您现在拥有了自己的 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.

  1. Create a .htaccess file in the root directory... domain.com/.
    Put the following in there:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?r=$1 [L,QSA] 
  1. 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"

  2. 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

  3. so run something like "SELECT ID FROM router WHERE URL=LOWER('{$r}')". ofcourse don't forget to check for sql injection attacks.

  4. You now have you ID and you can populate the page as you do at the moment.

Hope that helps :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文