Forum Moderators: phranque

Message Too Old, No Replies

reduce mod rewrite server load by parsing in PHP

instead of complicated rewrite rule, we parse REQUEST_URI in PHP

         

urbanzen

11:17 am on Jan 22, 2008 (gmt 0)

10+ Year Member



Hello webmasters,

Trying to bring across static url values to a dynamic script with variable $foo and $bar
I'm wondering which one is more efficient, and less on server load?

For example if given the url /request_uriA/long-text.html

Would it be better to
RewriteRule /request_uri([A-Z])/([\.]+)\.html some_script.php?foo=$1&bar=$2

OR..

RewriteRule /request_uri some_script.php

And do good ole regexp in some_script.php with parsing the $REQUEST_URI, str_posing the first alphabet after /request_uri, then retrieving long-text via preg-match

Average visits of 7000 daily let's say. with 1000 actual visit to some_script.php

scenario A have additional parse overhead of 7000 on a complicated URL, and scenario B have 7000 simple URL overhead, and 1000 script parse.

In this case...which one would be better, or am I over complicating things with miniscule comparisons?

Urbanzen

jdMorgan

2:45 pm on Jan 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The choice is largely a matter of 'style' or preference.

The interpreter for mod_rewrite is much smaller that that for PHP, and is also 'native' to the server, which means that the mod_rewrite method is a little faster and implies one less functional dependency. Therefore, there is no risk of losing URL-translation with the mod_rewrite method if, for example, you experience a problematic PHP upgrade. But on a modern server, I would not expect the server performance difference to be noticeable.

Also, if you are hosted on an Apache 2.x server, see the "AcceptPathInfo" directive, which can be used in place of mod_rewrite for your PHP-parsed method.

Jim

urbanzen

2:17 am on Jan 23, 2008 (gmt 0)

10+ Year Member



Thank you very much for the reply jdMorgan. I'm going to find out more about that module before I decide again!

pixeline

3:49 pm on Jan 23, 2008 (gmt 0)

10+ Year Member



It might be the php guy inside of me talking here, but if you're into optimizing, then forget that rewriting issue, it's not much, whereas the best thing to do first is to generate static pages out of your dynamic data. Caching, or html generation. and have that generation match the desired structure would be even better, so "products/foo/bar" actually exists.

Of course, you may have gone through that already.

pixeline

9:05 am on Jan 24, 2008 (gmt 0)

10+ Year Member



also, i just found this resource:

you can, via your .htaccess, send header telling the browser how long the downloaded files are to remain in the cache, so once downloaded it doesn't reload it, which contributes to lift your website charge

Apache .htaccess caching code


# 1 YEAR
<FilesMatch "\.(ico¦pdf¦flv)$">
Header set Cache-Control "max-age=29030400, public"
</FilesMatch>
# 1 WEEK
<FilesMatch "\.(jpg¦jpeg¦png¦gif¦swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
# 2 DAYS
<FilesMatch "\.(xml¦txt¦css¦js)$">
Header set Cache-Control "max-age=172800, proxy-revalidate"
</FilesMatch>
# 1 MIN
<FilesMatch "\.(html¦htm¦php)$">
Header set Cache-Control "max-age=60, private, proxy-revalidate"
</FilesMatch>

For fail-safe code implement this to the above

<IfModule mod_expires.c>
# any Expires Directives go here
</IfModule>

<IfModule mod_headers.c>
# any Header directives go here
</IfModule>

jdMorgan

3:42 pm on Jan 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would not call that "fail-safe."

By using the <IfModule> containers, you turn off the code if the server does not support the required module. But the question is, "Do you really want to do that?" If you use <IfModule>, the server will continue to successfully accept requests, but it will not append the cache-control headers because that code section is disabled. Therefore, using the <IfModule> container leads to a silent failure!

I would rather have the server throw an error message if required modules are not installed.

For your site, you decide. :)

Also, take advantage of regular expressions to make your patterns more efficient:


<FilesMatch "\.([b]jpe?g[/b]¦png¦gif¦swf)$">
<FilesMatch "\.([b]html?[/b]¦php)$">

Replace all broken pipe "¦" characters in the code above with solid pipes before use; Posting on this forum modifies the pipe characters.

Jim

urbanzen

2:10 am on Jan 25, 2008 (gmt 0)

10+ Year Member



Thank you both for the great advice. I'm going to read up on the FilesMatch directive and cache them better.