Martic.net

Talking in binary since 1980.

How does Wordpress handle friendly URLs?

Darko at 12:19 pm on Wednesday, August 12, 2009

Google URL magnifyNot so long time ago, I wrote about handling friendly URLs for your own site using Apache (it’s URL rewrite engine, and .htaccess file) and PHP – “Friendly URL, the easy way”.
In the meantime I was doing some changes to .htaccess file for one of my Wordpress installations, and what made me thinking was how does the Wordpress handle URLs.

If you open .htaccess file of your Wordpress, you’ll see that it’s URL rewriting engine is controled by just a few lines of code!

  1.  
  2. RewriteEngine on
  3. RewriteCond %{REQUEST_FILENAME} !-d
  4. RewriteCond %{REQUEST_FILENAME} !-f
  5. RewriteRule ^(.*)$ /index.php?page=$1
  6.  

Doing some googleing :) I run onto a helpful explanation on how this works. And the best way to understand this is to explain this is to simply comment each line of previous code:

  • 2nd line – initiating the Apache’s mod_rewrite module
  • 3rd line – If the request is for a real directory (one that exists on the server), index.php isn’t served.
  • 4th line – If the request is for a file that exists already on the server, index.php isn’t served.
  • 5th line – All other requests are sent to index.php; use $_GET['page'] to retrieve which page was acctually requested

Handling URL’s this way gives you ability to actually handle them from within your PHP code, which I found later on to be much more elegant and easier for maintance. Especially if you build your own PHP URL rewriting engine (or use some open-sourced) that is supported at backend (CMS) for handling missing/old URL’s, etc.

Basically all what your rewriting engine should do is parse URL and dynamicaly load pages and output them to the visitor.

 

1 Comment »

  1. Comment by Some guy
    December 7, 2009 @ 4:17 am

    Hey, thanks, just what I was looking for :)

RSS feed for comments on this post. TrackBack URL

Leave a comment

Enter this code

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 
 
Close
E-mail It
Socialized through Gregarious 42