如何在我自己的 PHP 应用程序中创建类似于 WordPress 的 URL 重写
我正在 Windows 2008R2 上使用 PHP5.3.6。
在 WordPress 中,我可以设置各种友好的 URL,最终路由到一个 PHP 页面。从外面看,它像很多页,但实际上只有一页。
在 web.config 文件(或 .htaccess)文件中,只有一条指令,而不是每页/文章有一个条目。
这意味着 PHP 正在某个地方查看 URL,将其与数据库(存在文章标题的地方)进行比较,然后透明地路由到页面。
所有这一切对最终用户都是透明的。
这是如何在非 WordPress PHP 站点中实现的?
I am working with PHP5.3.6 on Windows 2008R2.
In Wordpress, I can set all kinds of friendly URLs which ultimately route to one PHP page. From the outside, it looks like many pages, but is only one.
In the web.config file (or .htaccess) file, there is only one instruction as opposed to having one entry per page/article.
This means that somewhere PHP is looking at the URL, comparing it to the database (where the article titles exist) and then routing transparently to the page.
All of this is transparent to the end user.
How is this accomplished in a non wordpress PHP site?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是相关答案,涉及如何这样做。简而言之,您需要检查
$_SERVER['REQUEST_URI']
并解析它。下面是一个解析请求的简单示例(MVC 路由器通常是可配置的,并且可以路由和匹配许多不同的 URI 结构):
如果您的格式类似于
news/article-slig
您可以这样做(示例代码,有不太严格的方法可以做到这一点):此时您的 PHP 脚本知道如何解释请求。如果这是一个完整的 MVC 应用程序,路由器将加载匹配的控制器并将请求数据传递给它。如果您只是执行一个简单的单页脚本,加载一些数据,那么您的数据库调用就会随之而来。
如果请求无效,那么一个简单的
header()
调用就可以通知浏览器:并且任何数据输出都将是 404 页面的内容。
Here's a related answer that touches on how to do that. In brief, you'll want to check the
$_SERVER['REQUEST_URI']
and just parse that.Here's a simple example of parsing the request (MVC routers are usually configurable, and can route and match for many different URI structures):
If your format is something like
news/article-slig
you can do this (example code, there are less rigid ways to do this):At this point your PHP script knows how to interpret the request. If this were a full blown MVC application, the router would load the matching controller and pass the request data to it. If you're just doing a simple single page script, loading some data, then your DB calls would follow.
If the request is invalid, then a simple
header()
call can notify the browser:And any data output would be the content of your 404 page.
我不能保证 wordpress,我使用的一种方法是将 .htaccess 中的 404 重定向到 index.php,然后通过解析对该文件进行排序:
$sub
可用于屏蔽不存在的子域到特定文件。$file
可以在 switch 或 if 子句中使用,以根据文件名包含/重定向。显然,您需要确保别名不是文档根目录中的实际文件。
I can't vouch for wordpress, one method I have used is to redirect 404's in .htaccess to index.php and then have that file sort by parsing:
$sub
can be used to mask non existant subdomains to a specific file.$file
can be used in a switch or if clause to include / redirect based on file name.Obviously, you need to make sure that the alias' are not actual files in your doc root.
它称为路由(您还可以查看有关前端控制器模式的信息)。您可以通过使用服务器设置将所有请求重定向到单个文件来编写自己的实现,并解析该文件中的请求。例如,您还可以检查 Zend_Controller_Router 文档和源以了解其工作原理。
http://framework.zend.com/manual/en/zend.controller .router.html
http://framework.zend.com/manual/en/zend.controller.html
Its called Routing(you can also check info about Front Controller pattern). You can write your own implementation by redirecting all your requests to single file using server settings and parse request in this file. You can also check, for example, Zend_Controller_Router docs and sources to understand how it works.
http://framework.zend.com/manual/en/zend.controller.router.html
http://framework.zend.com/manual/en/zend.controller.html