views:

57

answers:

2

I'm planning to use large amounts of URL rewrites on my website, but I can't get familiar with Apache's mod_rewrite because it's difficult and poorly documented.

I heard there's some option that allows invoking various scripts for the rewriting purpose. Can you help me with it?

+6  A: 

Don't do it, at least not indiscriminately. You can redirect any request to PHP and do the parsing there. It's a tempting option, but it's horrible for performance because there's an expensive PHP process started for every request to every resource, including images, style sheets and so on.

Only when there is a lot of URL rewrites (like dozens, hundreds or thousands after a site move o restructuring) it may make sense to redirect them to a PHP script that does a database lookup. But even then you should know the basics of mod_rewrite in order to decide which requests to redirect to that script.

mod_rewrite is very finely documented, it just takes some getting into. I recommend at learning the basics.

One good resource to get started is this blog entry. There are numerous good questions on SO as well that are full of examples. The official docs are here.

Pekka
Unless all your PHP scripts have a single point of entry (an index.php), in which case this can possibly improve performance since there is less of a need for regex execution. But, with that said, always use the -F flag and exclude static content types that you don't want to serve dynamically (If all your images are static, exclude any request ending in .png|gif|jpg)...
ircmaxell
@ircmaxell exactly for that kind of info I think he needs to learn at least the basics of URL rewriting :)
Pekka
@ircmaxell, I'm not sure what I'm going to do with the site, but most likely, it _will_ have "single point of entry"
BlaXpirit
just to augment Pekka's fine answer a little, you can redirect everything which is not a file (-f) and not a directory (-d) to a PHP script for processing, this makes it somewhat lighter :)
nathan
@nathan Good point :) I had second thoughts whether what I wrote is not a bit too hard - after all, a `-f -d` solution is quick and easy to set up. On the other hand, unnecessary PHP processes are a mostly invisible waste of resources until the sysadmin calls and shuts the site down, so it's good to be warned of that. If the OP adds some more info about what his URLs look like, I'm sure somebody will write a sample .htaccess file.
Pekka
@Pekka, I actually agree with your post. The only thing I was pointing out that in some cases doing most of the rewriting in PHP land can be more efficient and powerful than doing it all in Apache. But yes, some needs to be done in Apache, so you might as well learn that. Either that, or go with Lighttpd and program the rewrite in a Lua module (mod_magnet)... But unless you already know Lua, Apache's rewrite mechanism may be a little easier to learn for the task...
ircmaxell
+2  A: 

You can use RewriteMap to call an external program. But, as Pekka said in his comment, you better not do this. It just makes it more complex and fragile. mod_rewrite is one of the best documented modules around and there are tons of tutorials, raging from simple to complex. I have done big website moves with many, many pages and a completely restructured URL schema. Even these can usually be done with a couple dozen rewrite rules or so.

Start with the rewrite guide.

Sander Marechal