TYPO free

home

fighting for TYPO free code

Rethinking the mod_rewrite rules (yes, again :-)

18 May 2009

A week ago it occured to me that it is nonsense to have mod_rewrite figure out if it has to rewrite stuff or not. It is better not to bother mod_rewrite with static items at all. This configuration started as an attempt to exclude '/typo3/*' from rewriting, but it evolved into a complete solution. 

We do NOT want to push the following requests to mod_rewrite:

  • /typo3/*
  • /typo3conf/*
  • /typo3temp/*
  • /fileadmin/*
  • /uploads/*
  • /clear.gif
  • /favicon.ico
  • /index.php

The rest (all realurl uri's) we DO want to push through to mod_rewrite.

If we can dream something up that can make this work, we no longer need mod_rewrite to check if something is a file, dir or link. Since all we push through is realurl stuff.

This is a task that is perfect for the Apache LocationMatch directive. The LocationMatch will only work in vhost or server context, not in htaccess files. It's tough to figure out how you can get stuff 'excluded' in a match, but a regex manual will explain all that.

This gives us the following snippet for inclusion in your vhost configuration:

<LocationMatch "^/(?!(typo3(/|conf|temp)|fileadmin|uploads|t3lib|clear.gif|index.php|favicon.ico))">
RewriteEngine on
RewriteRule .* /index.php [PT]
</LocationMatch>

You can now disable .htaccess in vhosts (AllowOverride None). Of course you don't have to, but Apache recommends this. If you do want to disable .htaccess, then make sure you also put extra configuration in your vhost for the .htaccess files you previously had in your installation.

You can see a big difference between rewrite activity if you turn on rewrite logging and look at the situation before and after the configuration change.

This configuration certainly looks more clear to me than the .htaccess currently in use. The big question is now:

How does the processing power for the LocationMatch stack up against the cpu cycles saved by not going through a lot of unnecessary  rewrite rules?

I have tried to measure this, but probably my server is too weak or I did not run the test for long enough or something else. So, I need your help! Please send me test results of before- and after statistics. I have tried in vain to measure against my own site using something like:

ab -n 2500 -c 10 www.domain.tld/typo3/

But I did not find a 'great' difference in requests per second. I do expect one though . . . but these results are not a convincing answer to the question above:

Old config:
Requests per second:    218.30 [#/sec] (mean)
New config:
Requests per second:    224.81 [#/sec] (mean)

What I would love to see is some server load monitoring images that show a great difference in load before and after the configuration change. Who can get us some?

Steffen 19 May 2009, 00:11
As already mentioned on T3DD09: Dmitry mentioned that htaccess performance loss is not significant any more.

Also, for TYPO3 4.3 and later it is supposed to enable
> AllowOverride Indexes FileInfo
This turns on some performance tweaks by using mod_expires for caching static files by default.
So if you turn off htaccess, make sure to put the mod_expires stuff also into your vhost/httpd.conf file.
@see: https://forge.typo3.org/repositories/entry/typo3v4-core/trunk/INSTALL.txt

I'll test your ideas once I find time...

P.S. As you can see I still follow your blog ;-)
Commenting is closed for this item