What is this? From this page you can use the Social Web links to save How does WordPress handle friendly URLs? to a social bookmarking site, or the E-mail form to send a link via e-mail.

Social Web

E-mail

E-mail It
August 12, 2009

How does WordPress handle friendly URLs?

Posted in: Programming,SEO, SEM

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.

 


Return to: How does WordPress handle friendly URLs?