重写 url 以隐藏 index.php 并使查询变得更好

发布于 2024-12-07 16:27:45 字数 1004 浏览 1 评论 0 原文

我使用自定义 MVC 架构开发了项目。我是 Apache 世界的新手,所以我很乐意在这件事上得到帮助。我在网上找到了很多教程,但没有人喜欢我的兴趣。

我有这样的网址: http://knjiskicrv.comoj.com/index .php?page=book&id=1

我想这样显示:http://knjiskicrv.comoj.com/book/id/1

或者这样:http://knjiskicrv.comoj.com/faq

如果没有页面< /code> 在查询(http://knjiskicrv.comoj.com/index.php)中,我想显示: http://knjiskicrv.comoj.com/

查询中也没有 page (http://knjiskicrv.comoj.com/index.php?category= 2),应该是这样的 http://knjiskicrv.comoj.com/category/2

希望有人能提供帮助。谢谢。

I had developed project with custom MVC architecture. And I am new to Apache world, so I would appreciate help with this matter. On a Web I had found lots of tutorials, but no one like mine interests.

I have URL like this: http://knjiskicrv.comoj.com/index.php?page=book&id=1

I would like to be display like this: http://knjiskicrv.comoj.com/book/id/1

Or this: http://knjiskicrv.comoj.com/index.php?page=faq
Into this: http://knjiskicrv.comoj.com/faq

If there is no page in query (http://knjiskicrv.comoj.com/index.php), I would like to show: http://knjiskicrv.comoj.com/

Also with no page in query (http://knjiskicrv.comoj.com/index.php?category=2), it should be like this http://knjiskicrv.comoj.com/category/2

Hope someone will help. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

小梨窩很甜 2024-12-14 16:27:45

实际上,你的问题是一个两步问题。首先需要了解什么是MVC中的“路由”。如果您有自己的 MVC 之类框架的实现,并且不支持路由,那么这可能意味着您以前甚至不知道它是如何工作的。 (悲伤但真实)

在 MVC 框架中,您使用路由器设置路由,路由器会为您分析 url,并说“嘿,我发现这个 url 与您的请求匹配,请继续使用它”。

然后,您的控制器收到一个路由到自身的请求,并根据他认为合适的方式解析 url。例如使用explode('/', $_SERVER['REQUEST_URI']),然后读取url的不同部分以映射到预期的变量。

所有这些都是非常理论化的,因为有无数种方法可以以自定义方式实现它。您唯一需要使用的是一点 mod_rewrite 魔法,将所有请求传递到将路由所有内容的 index.php。查看下面的网址以了解 mod_rewrite,这是一个非常复杂的主题:

http://www.addedbytes.com/for-beginners/url-rewriting-for-beginners/

我通常会去但我无法从家里访问它是一些东西像这样:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^assets/
RewriteRule .* index.php

这会将所有流量重定向到index.php,然后您可以使用$_SERVER['REQUEST_URI']来分析请求。资产/文件夹中的所有内容都不会被触及并正常工作。

请注意,我用我的帽子构建了该部分,它可能不起作用......

Actually, your problem is a two step proble. You first need to understand what is "Routing" in MVC. If you have your own implementation of an MVC like framework and you don't support routing, then it probably means you didn't even know how it worked before. (Sad but true)

In an MVC framework you setup routes using a ROUTER and the router analyses the urls for you saying HEY, i found this url that matches your request, go ahead and do work with it.

So then, your controller receives a request to route into itself and PARSES the url as he sees fit. Such as using explode('/', $_SERVER['REQUEST_URI']) and then reading the different parts of the url to map to expected variables.

All of this is very theoretical because there are ZILLIONS of way to implement it in a custom way. The only thing you will have to use is a little mod_rewrite magic to pass all requests to your index.php that will route everything. Look at the url below to learn about mod_rewrite, it's a VERY COMPLEX subject:

http://www.addedbytes.com/for-beginners/url-rewriting-for-beginners/

What i usually go for but i don't have access to it from home is something like this:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^assets/
RewriteRule .* index.php

This will redirect all traffic to index.php and you can then use $_SERVER['REQUEST_URI'] to analyze the request. Everything in assets/ folder will not be touched and work correctly.

Note, i built that part off my hat, it might not work...

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