URL处理 - PHP VS阿帕奇重写 [英] URL handling – PHP vs Apache Rewrite

查看:137
本文介绍了URL处理 - PHP VS阿帕奇重写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我让一个单一的PHP脚本处理所有传入的URL。这个PHP脚本然后解析URL并加载该网址的具体处理程序。像这样的:

 如果(URI ===/)
{
    require_once(root.php);
}
否则,如果(URI ===/店)
{
    require_once(shop.php);
}
否则,如果(URI ===/接触)
{
    require_once(contact.php);
}...其他
{
    require_once(404.php);
}

现在,我一直在想,这其实是非常低效的,打算一旦我的网站被访问更多的时候需要很多不必要的处理能力。所以我想,为什么不与mod_rewrite的Apache的范围内做到这一点,让阿帕奇直接加载PHP脚本:

 重写规则^ $ root.php [L]
重写规则^ $店shop.php [L]
...

但是,因为我有很多这些网址的,我只是想做出改变,如果这真的是值得的。

所以,这里是我的问题:什么选择是更好的(效率明智及其他方面),为什么

顺便说一句,我绝对要保持URL方案,而不是简单地让脚本通过自己的实际文件名(something.php)进行访问。


解决方案

  

所以,这里是我的问题:什么选择是更好的(效率明智及其他方面),为什么


如果每个资源都有通过一个基于PHP检查运行,如您在您的评论说:


  

一些资源只提供给已登录的用户,所以我得到检查饼干和第一登录状态,那么我为他们服务与ReadFile的()。


,那么你的确可以使用PHP端逻辑来处理事情:一个PHP实例将要开始无论如何,这使得在Apache中解析URL还很有争议的性能改进

如果你有这样做的静态资源不是的需要的任何会议或其他PHP端检查,你绝对应该处理.htaccess文件如果可能的路由,因为你避免启动一个单独的PHP程序每一个资源。但在你的情况下,将不适用。

一些想法,以提高性能:


  • 考虑是否真的每个的资源需要通过基于PHP的认证保护。样式表或者一些图像不能够公开,节能性能密集型的PHP程序?


  • 尝试与缩小资源后的文件成为可能,例如,受污染减量的所有样式表合为一体,并采用CSS精灵,以减少图像的数量。


  • 我听说 nginx的是ppared来处理这种特定类型的场景中更好的$ P $ - 至少有人告诉我它可以非常有效地处理文件的交付认证检查已经完成之后,而不必依赖于PHP的 ReadFile的()


Currently I let a single PHP script handle all incoming URLs. This PHP script then parses the URL and loads the specific handler for that URL. Like this:

if(URI === "/")
{
    require_once("root.php");
}
else if(URI === "/shop")
{
    require_once("shop.php");
}
else if(URI === "/contact")
{
    require_once("contact.php");
}

...

else
{
    require_once("404.php");
}

Now, I keep thinking that this is actually highly inefficient and is going to need a lot of unnecessary processing power once my site is being visited more often. So I thought, why not do it within Apache with mod_rewrite and let Apache directly load the PHP script:

RewriteRule ^$ root.php [L]
RewriteRule ^shop$ shop.php [L]
...

However, because I have a lot of those URLs, I only want to make the change if it really is worth it.

So, here's my question: What option is better (efficiency-wise and otherwise) and why?

Btw, I absolutely want to keep the URL scheme and not simply let the scripts accessible via their actual file name (something.php).

解决方案

So, here's my question: What option is better (efficiency-wise and otherwise) and why?

If every resource has to run through a PHP based check, as you say in your comment:

some resources are only available to logged in users, so I get to check cookies and login state first, then I serve them with readfile().

then you can indeed use PHP-side logic to handle things: A PHP instance is going to be started anyway, which renders the performance improvement of parsing URLs in Apache largely moot.

If you have static resources that do not need any session or other PHP-side check, you should absolutely handle the routing in the .htaccess file if possible, because you avoid starting a separate PHP process for every resource. But in your case, that won't apply.

Some ideas to increase performance:

  • consider whether really every resource needs to be protected through PHP-based authentication. Can style sheets or some images not be public, saving the performance-intensive PHP process?

  • try minifying resources into as few files as possible, e.g. by minifying all style sheets into one, and using CSS sprites to reduce the number of images.

  • I've heard that nginx is better prepared to handle this specific kind of scenario - at least I'm told it can very efficiently handle the delivery of a file after the authentication check has been done, instead of having to rely on PHP's readfile().

这篇关于URL处理 - PHP VS阿帕奇重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆