Note that there are some explanatory texts on larger screens.

plurals
  1. POPhp handling vs Apache RewriteRules and RegExp: which one is quicker?
    text
    copied!<p>I've already read <a href="https://stackoverflow.com/questions/6961483/url-handling-php-vs-apache-rewrite">this</a> but it doesn't answer to my question.</p> <p>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 <em>though I didn't test its efficiency</em>. It was dealing with lot of array (<code>array_push()</code>, <code>array_key_exists()</code> and so on)</p> <p>I've rewritten the whole Php file, now it create its own "cache" file, <strong>and</strong> almost all the work is done by Apache before going to the Php file. The Php file only analyzes what's in the <code>_GET</code>.</p> <p>But I'm worrying about one thing: are RegExp fast? In my previous Php file, I was playing a lot with <code>array_xx()</code> function, but there were absolutely <strong>no Apache RewriteRules</strong> before.</p> <p>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.</p> <p>This is kinddof advices and warnings about performance on "Apache RegExp RewriteRules" and "Php handling URLs instead of Apache".</p> <p>Here are all my RewriteRule, and I'm just wondering whether they may slow down my Apache Webserver <em>more than my previous Php file</em>.</p> <pre><code>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}&amp;Pt=%{ENV:PLOCAL}&amp;Pt_cm=${ts:cm.}&amp;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}&amp;Plocal=%{ENV:PLOCAL}&amp;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&amp;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&amp;id=$2&amp;chaine_vation=$3 [QSA,L] RewriteCond %{HTTP_HOST} ^www\.(.*) [NC] RewriteRule /d-contact/numero-([0-9]+)-([a-z0-9]+)/$ /d_message.php?id=$1&amp;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&amp;id=$2 [QSA,NC,E=ct:${mapcts:$1|notfound}] RewriteRule ^/d-ann/(.*)/numero-([0-9]+)-([a-z0-9]+)/$ /d-ann/?val_ct=$1&amp;id=$2&amp;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&amp;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}&amp;dp=%{ENV:dp}&amp;ct=%{ENV:ct} [QSA,L] RewriteRule .* - [L,R=404] </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload