2

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 ?

1 Answer 1

3

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'].

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! I figured it could be something like that but denied it as i thought no one can be certain server variables are available everywhere. $_SERVER['REQUEST_URI'] is actually a reliable variable, that is present on all setups?
$_SERVER['REQUEST_URI'] does not contain the full URI but only the URI path and query.
@pixeline: $_SERVER['REQUEST_URI'] is provided by the web server to PHP; most common web servers (including Apache and IIS) support it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.