PHP 处理与 Apache RewriteRules 和 RegExp:哪个更快? [英] Php handling vs Apache RewriteRules and RegExp: which one is quicker?

查看:13
本文介绍了PHP 处理与 Apache RewriteRules 和 RegExp:哪个更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了这个但它没有回答我的问题.

I've already read this but it doesn't answer to my question.

这是我的场景:我一直在开发自己引以为豪的框架(多语言、模板等).但我不得不面对一个问题:多语言和模板处理是在 PHP 中完成的.800行代码,分析host看哪里看,读模板等等.它很乱,我很确定它效率不高尽管我没有测试它的效率.它正在处理大量数组(array_push()array_key_exists() 等等)

Here's my scenario: I've been working on my own framework which I'm proud of (multilanguage, templating, and so on). But I had to face one problem: the multilanguage and template handling was done in Php. 800 lines of code, analyze host to see where to look to read the templates and so on. It was messy and I'm pretty sure it wasn't efficient though I didn't test its efficiency. It was dealing with lot of array (array_push(), array_key_exists() and so on)

我已经重写了整个 Php 文件,现在它创建了自己的缓存"文件,并且几乎所有工作都由 Apache 在转到 Php 文件之前完成.Php 文件只分析 _GET 中的内容.

I've rewritten the whole Php file, now it create its own "cache" file, and almost all the work is done by Apache before going to the Php file. The Php file only analyzes what's in the _GET.

但我担心一件事:RegExp 快吗?在我之前的 Php 文件中,我经常使用 array_xx() 函数,但之前绝对没有 Apache RewriteRules.

But I'm worrying about one thing: are RegExp fast? In my previous Php file, I was playing a lot with array_xx() function, but there were absolutely no Apache RewriteRules before.

我不了解 RegExp 的性能,也不了解 Apache RewriteRules 以及初始化环境变量所需的时间.我经常这样做(见下文).所以,也许在这些解释之后,有人会告诉我没问题,你的 RegExp 并不复杂,Apache 处理得很快",或者立即停止,你可能会遇到 RegExp 的麻烦"等等.

I don't know about RegExp performance, and I don't know about Apache RewriteRules and the time it takes to initialize environment variables. I do this a lot (see after). So, maybe, after those explanations, someone could tell me either "no problem, your RegExp are not complex, Apache handles this quickly", or "stop this immediately, you may get in trouble with RegExp" and so on.

这是关于Apache RegExp RewriteRules"和Php 处理 URLs 而不是 Apache"的性能的一些建议和警告.

This is kinddof advices and warnings about performance on "Apache RegExp RewriteRules" and "Php handling URLs instead of Apache".

这是我所有的 RewriteRule,我只是想知道它们是否会比我以前的 Php 文件拖慢我的 Apache 网络服务器.

Here are all my RewriteRule, and I'm just wondering whether they may slow down my Apache Webserver more than my previous Php file.

RewriteMap maprns dbm:/rns.map
RewriteMap mapdps dbm:/dps.map
RewriteMap mapcts dbm:/cts.map
RewriteMap ts dbm:/ts.map
RewriteRule /404.php - [QSA,E=PLOCAL:${ts:www.}]
RewriteCond %{ENV:PLOCAL} ^default.([a-zA-Z0-9-]+).$
RewriteRule /404.php - [QSA,E=L:%1,E=PLOCAL:${ts:%1.}]
RewriteCond %{ENV:PLOCAL} !^$
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9-]+.)+([a-zA-Z0-9-]+).+(fr|com|net|org|eu)$
RewriteRule (.*) /404.php?L=%{ENV:L}&Pt=%{ENV:PLOCAL}&Pt_cm=${ts:cm.}&h_static=%{ENV:L}.s.%2.%3 [QSA,L]
RewriteCond %{HTTP_HOST} ((([a-zA-Z0-9-]+).)+)(s|static).(([a-zA-Z0-9-]+.)+)([a-zA-Z0-9-]+).+(fr|com|net|org|eu)$
RewriteRule (.*) - [QSA,E=L:%3,E=PLOCAL:${ts:%1%5|notfound},E=Pcm:${ts:cm.%5},E=STATIC:1]
RewriteCond %{ENV:PLOCAL} ^$
RewriteCond %{HTTP_HOST} ((([a-zA-Z0-9-]+).)+)(s|static).([a-zA-Z0-9-]+).+(fr|com|net|org|eu)$
RewriteRule (.*) - [QSA,E=L:%3,E=PLOCAL:${ts:%1|notfound},E=Pcm:${ts:cm.},E=STATIC:1]
RewriteCond %{ENV:PLOCAL} ^$
RewriteCond %{HTTP_HOST} (([a-zA-Z0-9-]+).)((([a-zA-Z0-9-]+).)+)([a-zA-Z0-9-]+).+(fr|com|net|org|eu)$
RewriteRule (.*) - [QSA,E=L:%2,E=PLOCAL:${ts:%1%3|notfound},E=Pcm:${ts:cm.%3}]
RewriteCond %{ENV:PLOCAL} ^$
RewriteCond %{HTTP_HOST} (([a-zA-Z0-9-]+).)([a-zA-Z0-9-]+).+(fr|com|net|org|eu)$
RewriteRule (.*) - [QSA,E=L:%2,E=PLOCAL:${ts:%1|notfound},E=Pcm:${ts:cm.}]
RewriteCond %{ENV:PLOCAL} ^default.([a-zA-Z0-9-]+).(([a-zA-Z0-9-]+.)+)
RewriteRule (.*) - [QSA,E=L:%1,E=PLOCAL:${ts:%1.%2|notfound},E=Pcm:${ts:cm.%2}]
RewriteCond %{ENV:PLOCAL} ^default.([a-zA-Z0-9-]+).$
RewriteRule (.*) - [QSA,E=L:%1,E=PLOCAL:${ts:%1.|notfound},E=Pcm:${ts:cm.}]
RewriteCond %{ENV:PLOCAL} ^$ [OR]
RewriteCond %{ENV:PLOCAL} notfound
RewriteRule .* - [R=404,L]
RewriteRule (.*) $1?L=%{ENV:L}&Plocal=%{ENV:PLOCAL}&Pcm=%{ENV:Pcm} [QSA]
RewriteCond %{ENV:STATIC} !^$
RewriteRule (.*)(.(css|js|pdf|jpg|jpeg|gif|png)){1}$ $1$2 [QSA,E=EXT:$3]
RewriteCond %{ENV:EXT} (jpg|jpeg|gif|png)
RewriteRule (.*) - [QSA,E=EXT:img]
RewriteCond %{ENV:STATIC} !^$
RewriteCond %{ENV:EXT} !([a-z]+)
RewriteRule .* - [L,R=404]
RewriteCond %{ENV:STATIC} !^$
RewriteCond %{ENV:EXT} (css|js)$
RewriteRule (.*) /%1.php?%1=$1&static=1 [QSA,L]
RewriteCond %{ENV:STATIC} !^$
RewriteCond %{DOCUMENT_ROOT}/%{ENV:PLOCAL}/%{ENV:EXT}%{REQUEST_FILENAME}  -f
RewriteRule  ^(.+) %{DOCUMENT_ROOT}/%{ENV:PLOCAL}/%{ENV:EXT}%{REQUEST_FILENAME} [QSA,L]
RewriteCond %{ENV:STATIC} !^$
RewriteCond %{DOCUMENT_ROOT}/%{ENV:Pcm}/%{ENV:EXT}%{REQUEST_FILENAME}  -f
RewriteRule  ^(.+) %{DOCUMENT_ROOT}/%{ENV:Pcm}/%{ENV:EXT}%{REQUEST_FILENAME} [QSA,L]
RewriteCond %{ENV:STATIC} !^$
RewriteRule .* - [L,R=404]
RewriteRule ^/$ /index.php [QSA,L]
RewriteRule /d-envoyer-lte-par-mail/ /d_envoyer_lte_par_mail.php [QSA,L]
RewriteRule /d-creer-editer/ /d_creer_editer.php [QSA,L]
RewriteRule /d-mail-ver/(.*)/$ /d_mail_ver.php?chaine_vation=$1 [QSA,L]
RewriteRule /d-mail-ver/ /d_mail_ver.php [QSA,L]
RewriteRule /i/lg/$ /i/lg.php [QSA,L]
RewriteRule /i/lg-ver/$ /i/lg_ver.php [QSA,L]
RewriteCond %{HTTP_HOST} ^s.(.*) [NC]
RewriteRule /contact-([0-9]+)-([0-9]+)-([a-z0-9]+).png$ /d_image_telephone.php?no=$1&id=$2&chaine_vation=$3 [QSA,L]
RewriteCond %{HTTP_HOST} ^www.(.*) [NC]
RewriteRule /d-contact/numero-([0-9]+)-([a-z0-9]+)/$ /d_message.php?id=$1&chaine_vation=$2 [QSA,L]
RewriteRule ^/d-(dtl|ann)/offre/(.*)/$ /d-$1/$2/$3?d_type=1 [QSA,NC]
RewriteRule ^/d-(dtl|ann)/demande/(.*)/$ /d-$1/$2/$3?d_type=2 [QSA,NC]
RewriteRule ^/d-dtl/(.*)/numero-([0-9]+)/$ /d-dtl/?val_ct=$1&id=$2 [QSA,NC,E=ct:${mapcts:$1|notfound}]
RewriteRule ^/d-ann/(.*)/numero-([0-9]+)-([a-z0-9]+)/$ /d-ann/?val_ct=$1&id=$2&chaine_ann=$3 [QSA,NC,E=ct:${mapcts:$1|notfound}]
RewriteCond %{ENV:ct} ([0-9]+)
RewriteRule /d-(dtl|ann)/ /d_$1.php?ct=%{ENV:ct} [QSA,NC,L]
RewriteRule ^/d/offres/(rn|dp|ct)/(.*)/$ /d/$1/$2/?d_type=1 [QSA,NC]
RewriteRule ^/d/demandes/(rn|dp|ct)/(.*)/$ /d/$1/$2/?d_type=2 [QSA,NC]
RewriteRule ^/d/(.*)/d-([0-9]+)-a-([0-9]+)/$ /d/$1/$2/?start=$3&end=$4 [QSA,NC]
RewriteRule ^/d/rn/([a-z-\_0-9]+)/(.*)/$ /d/$2/?val_rn=$1 [QSA,NC,E=rn:${maprns:$1|notfound}]
RewriteRule ^/d/rn/(.*)/$ /d/?val_rn=$1 [QSA,NC,E=rn:${maprns:$1|notfound}]
RewriteRule ^/d/dp/(.*)/$ /d/?val_dp=$1 [QSA,NC,E=dp:${mapdps:$1|notfound}]
RewriteRule ^/d/ct/(.*)/$ /d/?val_ct=$1 [QSA,NC,E=ct:${mapcts:$1|notfound}]
RewriteCond %{ENV:rn} notfound [OR]
RewriteCond %{ENV:dp} notfound [OR]
RewriteCond %{ENV:ct} notfound
RewriteRule .* - [L,R=404]
RewriteCond %{SCRIPT_FILENAME} /d/
RewriteCond %{QUERY_STRING} !start=(.+)
RewriteRule (.*) $1?start=1 [NC,QSA]
RewriteCond %{SCRIPT_FILENAME} /d/
RewriteCond %{QUERY_STRING} !end=(.+)
RewriteRule (.*) $1?end=20 [NC,QSA]
RewriteCond %{ENV:rn} ([0-9]+) [OR]
RewriteCond %{ENV:dp} ([0-9]+) [OR]
RewriteCond %{ENV:ct} ([0-9]+)
RewriteRule /d/ /d_lte.php?rn=%{ENV:rn}&dp=%{ENV:dp}&ct=%{ENV:ct} [QSA,L]
RewriteRule .* - [L,R=404]

推荐答案

这将取决于您的服务器、内存等.唯一确定的方法是运行性能基准测试,看看 httperf 一个.

It will depend on your servers, memory, etc. The only way to know for sure is to run a performance benchmark, take a look at httperf for one.

主观上,我可以告诉你,我们的 .htaccess 中大约有 450 行重写规则在起作用,虽然尽可能少总是最好的,但重写规则目前还不是应用程序的瓶颈(我们每秒处理数千个请求).

Subjectively, I can tell you that we have about 450 lines of rewrite rules in our .htaccess at work, and while it's always best to have as little as possible, the rewrite rules are NOT the bottleneck of the application by far (and we serve some thousands of requests per second).

所以,考虑到这一点,我不会太担心.在到达重写规则是系统中最慢的部分之前,您更有可能遇到未优化的数据库、缓存不足和许多其他问题.

So, with that in mind, I wouldn't worry too much about it. You're more likely to run into problems with unoptimized databases, insufficient caching, and many other things, before you get to the point where rewrite rules are the slowest part of your system.

您的重写规则集看起来确实有些混乱,因此我会尝试花一些时间将其组织成更有意义的 url 结构(并从旧 URL 添加 301 重定向).您可以查看在 Kohana、Codeigniter 或 Symfony 等常见 HMVC 框架中如何处理路由.

Your rewrite rule set does seem kind of disorganized, so I would try to spend some time organizing it into a more meaningful url structure (and add 301 redirects from old URL's). You can look into how routing is handled in common HMVC frameworks like Kohana, Codeigniter, or Symfony.

这篇关于PHP 处理与 Apache RewriteRules 和 RegExp:哪个更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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