Search code examples
wordpress.htaccessmod-rewritepermalinks

Wordpress "search engine friendly" urls ( permalinks) implementation


I had the pleasure to take a peak at Wordpress source code; i was wondering how they managed the custom url feature, but i couldn't really get it.

The rewrite rules inside wordpress .htaccess file simply redirect all requests to index.php.

After that, it's a mystery to me: how do they make example.com/this/title/is/cool/ match index.php?p=233 ?


Solution

  • Once it's redirected to index.php, that file can look at $_SERVER['REQUEST_URI'] to determine what is in the this/title/is/cool portion, and then go look up what page to serve from a database, since the REQUEST_URI lists the full URI string, even though the actual page url that was redirected to is only the first portion of it.

    For instance,

    http://www.example.com/foo/bar/
    

    gets rewritten to

    http://www.example.com/index.php/foo/bar/
    

    This will actually result in http://www.example.com/index.php being loaded, but $_SERVER['REQUEST_URI'] will have the full /index.php/foo/bar/ within it.

    Some apps use a different approach, they use .htaccess to just take the trailing "directories" and put them into the query string, so that the rewrite becomes something like this:

    http://www.example.com/index.php?path=/foo/bar/
    

    in which case the supplied path is available in _GET['path'].