使用长长的 .htaccess RewriteRule 列表还是直接在 PHP 中使用单个包罗万象?

发布于 2024-12-29 15:54:02 字数 506 浏览 2 评论 0原文

假设我使用 .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 技术交流群。

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

发布评论

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

评论(2

天赋异禀 2025-01-05 15:54:02

如果这满足了您的要求,那么使用大量重写规则就没有问题了。我已经看到重写规则填满了我的整个显示器,然后是一些 - 大量列表没有问题,除非你对它们有问题。

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.

嘿咻 2025-01-05 15:54:02

您可能想看看 ZendRouter - 即使您没有使用框架,您也可能想遵循他们的想法。

http://framework.zend.com/manual/en/zend.controller .router.html

本质上 .htaccess 将所有请求路由到 index.php 文件,但与正则表达式的任何匹配除外:

RewriteRule !\.(js|ico|gif|jpg|png|css|html)$ index.php

或任何非文件/目录(尽管我认为检查是否有一个稍高的成本文件/目录存在而不是正则表达式请求的 URL)

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

一旦您利用了这种类型的路由 - 您可以为 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:

RewriteRule !\.(js|ico|gif|jpg|png|css|html)$ index.php

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)

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

Once you've leveraged this type of routing - you can define a standard regex for URLs to map to controllers/actions

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