在 PHP MVC 框架中处理 3xx/4xx 请求?

发布于 2024-08-05 11:29:06 字数 475 浏览 6 评论 0原文

如果说您正在利用流行的:

RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L,QSA]

并且您正在尝试使您的 mvc Web 框架处理 404,如果您没有找到指定 URI 的相应视图,您是否只需在 php 应用程序本身内手动发送 404 http 标头,正确的? Zend/Kohana 和其他框架就是这样做的吗?

另一个问题,假设您重写了整个网站并且想要设置 301。您会在配置路由的地方(像 Django 中的 urls.py 那样的正则表达式样式)还是在虚拟主机指令中执行此操作?

基本上,如果我的 Web 框架正在处理路由,我是否必须修改 VirtualHost 中的 RewriteRules?

If say you're utilizing the popular:

RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L,QSA]

And you're trying to make your mvc web framework handle 404's, if you don't find a corresponding view for a specified URI would you just manually send 404 http headers within the php application itself, right? Is this how it's done in Zend/Kohana and other frameworks?

Another question, let's say you rewrote an entire site and you want to setup 301s. Would you do that in the place where you configure your routes ( regex style like urls.py in Django ) or in the Virtualhost Directive?

Basically, should I ever have to modify the RewriteRules in the VirtualHost if my web framework is handling the routing?

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

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

发布评论

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

评论(1

迷爱 2024-08-12 11:29:06

一般来说,对于 404,是的。

至于进行永久重定向,我认为这取决于上下文。如果我的路由解决方案支持以任何理智的方式执行此操作,我可能会选择它,除非我预计会遇到大的扩展问题。

意思是,如果我正在处理大量流量,并且有很多静态内容要提供服务(但没有大到我使用除了 apache 作为 Web 服务器之外的任何东西),我可能最终会在 fcgi 下运行 php 以保持 apache 进程的精简(并使用比 prefork 更好的 MPM)。在那种情况下,我可能最终会短路 URL 并让 Web 服务器进行重定向。

但即使在这种情况下,也没有理由不首先在路由框架中完成此操作。

这样,如果请求漏掉了,即使速度变慢,事情仍然可以进行。

On 404's, generally speaking, yes.

As far as doing permanent redirection, I suppose it depends on context. If my routing solution supported doing it in any sane manner, I'd probably go for it unless I expected to run into big scaling problems.

Meaning, if I was handling heavy traffic, and had lots of static stuff content to serve (but not so massive that I was using anything but apache as a web server), I might end up running php under fcgi to keep apache processes thin (and use a better MPM than prefork). In that sort of world, I'd probably end up short-circuiting URLs and have the web server do the redirection.

But even in those sorts of cases, there's no reason not to have done it in your routing framework first.

That way, if a request slips through, things still work, even if they slow down.

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