Monday, May 18, 2009

How WordPress URLs Work

Ah, ha! How silly I feel now that I know the truth. I've been trying to understand how WordPress URL rewriting works, thinking incorrectly that it relied on Apache's mod_rewrite to do the dirty work. That is, however, not the case!

I've googled it ("how do WordPress URL rewrites work?") many a time, but never got a clear answer. I wonder if Wolfram Alpha knows now :P

WordPress does rely on mod_rewrite to invoke index.php, but doesn't use it to do any of the rewriting itself. The .htaccess file that WP uses contains this:


<ifmodule>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</ifmodule>


The !-f and !-d conditions instruct Apache to rewrite "whatever isn't an already-existing File or Directory." Just FYI, the [L] flag means, "this is the Last rule."

This gives me hope. Now, I understand that it is actually possible (because it's in practice) for me to parse and rewrite my own URLs. Doing my own URL processing is starting to make a lot of sense for a project that is right now a bunch of hen-scratch on paper.

No comments:

Post a Comment