I’m studying how to do url rewriting in a LAMP framework. I began my research by studying wordpress code. I looked at wordpress’s .htaccess and saw this:
# BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress
I was surprised AND delighted! I was surprised because I didn’t see any of those regular expression-like rules. I was delighted because I can delay learning how .htaccess files work (rushing for deadline), use the above script and parse the $_SERVER variable in PHP for url data.
So my question is, what are the advantages/disadvantages of managing url rewrites in the .htaccess file? And what are the advantages/disadvantages of managing url rewrites in the PHP code?
Doing so in PHP will lead to the loading of, parsing of and execution of more PHP scripts/code. If you do go this way, don’t get to the right page with external redirects. Remember that PHP scripts are essentially compiled every time they’re hit (opcode caching notwithstanding).
mod_rewrite will be much more efficient at doing this. It will also force you to keep such rules fairly simple since mod_rewrite can only use regular expressions (and a few other things) whereas in PHP you could potentially do anything (like doing database lookups to find the right page, etc). Not good.
Regular expressions aren’t hard. Usually it’s just a case of:
90%+ of my rewrite rules look like that. Pretty basic stuff.