使用长长的 .htaccess RewriteRule 列表还是直接在 PHP 中使用单个包罗万象?
假设我使用 .htaccess 文件来获得干净的链接,但我有一个很长的 RewriteRules 列表(比如 20 个)来执行类似的操作,
domain.com/user/adam --> domain.com/user.php?need=user&user=adam
domain.com/test.js --> domain.com/index.php?need=code&file=test.js
domain.com/test.js/edit --> domain.com/index.php?need=code&file=test.js&action=edit
在 PHP 中使用捕获所有 RewriteRule 并在斜杠/解析上进行分割和直接分割是否更好?还是长长的RewriteRule列表更好?
更好我想我的意思是更快、更稳健。我知道如果把所有东西都包起来的话,维护会更好。
另外,你能推荐一个关于这样做的 php 类示例/文章吗?
Say im using .htaccess file for clean looking links, but that I have a long list (say 20) of RewriteRules to do things like this
domain.com/user/adam --> domain.com/user.php?need=user&user=adam
domain.com/test.js --> domain.com/index.php?need=code&file=test.js
domain.com/test.js/edit --> domain.com/index.php?need=code&file=test.js&action=edit
Is it better to use a catch all RewriteRule and split on the slash/ parse and direct in PHP? Or is the long list of RewriteRule better?
Better I guess I mean faster, and more robust. I know that upkeep would be better with the catch all.
Also, can you recommend a php class example/article on doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果这满足了您的要求,那么使用大量重写规则就没有问题了。我已经看到重写规则填满了我的整个显示器,然后是一些 - 大量列表没有问题,除非你对它们有问题。
If this answers what your asking, there is no problem using a massive list of rewrite rules. I have seen rewrite rules that fill my whole monitor and then some - there is no problem with a massive list unless you have a problem with them.
您可能想看看 ZendRouter - 即使您没有使用框架,您也可能想遵循他们的想法。
http://framework.zend.com/manual/en/zend.controller .router.html
本质上 .htaccess 将所有请求路由到 index.php 文件,但与正则表达式的任何匹配除外:
或任何非文件/目录(尽管我认为检查是否有一个稍高的成本文件/目录存在而不是正则表达式请求的 URL)
一旦您利用了这种类型的路由 - 您可以为 URL 定义标准正则表达式以映射到控制器/操作
You might want to look at ZendRouter - even though you're not using a framework, you may want to follow their ideas.
http://framework.zend.com/manual/en/zend.controller.router.html
Essentially .htaccess routes all requests to the index.php file with the exception of any matches to the RegEx:
Or any non files / directories (though I think there is a slightly higher cost to check if a file/directory exists rather than regex the requested URL)
Once you've leveraged this type of routing - you can define a standard regex for URLs to map to controllers/actions