• Hello,

    When normal dynamic links are used, the WP script knows what post the user is asking for by checking the value passed by the URL i.e. $_GET[‘p’] has the post ID, and then it searches the DB for the post with that ID.

    But when permalinks are used no info are passed by the URL. Let’s take an example permalink of a post:
    http://myblog.com/misc/my-post-title”
    Now when I visit that link how does the WP script know what post I am asking for?
    It can’t use the $_GET variable because no info is passed.

    I know that .htaccess redirects the permalink to myblog.com/index.php, but after that how does it know what post is being requested when no “GET” data is passed by the URL?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    There’s a large quantity of code in WordPress that converts that URL into the proper internal parameters (called “query variables” internally). This is all the “rewrite” stuff in the wp-include directory.

    Thread Starter aha7

    (@aha7)

    But what’s the general idea?

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Hmm.. Okay, general idea is that it gets the URL you’re using and then does a whole lot of string parsing. From that, it determines what page you’re trying to reach, and then sets the internal query variables to be the same as if you had used the GET.

    For example, if you try to go to http://example.com/2007/09/28/ , then it sees that the string represents a date, so it sets year, month, and day accordingly. The permalink choice you make on Options->Permalink is a fallback which it tries when the other base options fail to match.

    These query variables are also set when you explicitly set them via the GET approach. So all the rest of the code is the same regardless, the pretty permalinks are converted internally to the same sort of things.

    All this code is in the WP-Rewrite stuff. rewrite.php or something like that. This replaces the old approach of actually using mod_rewrite with Apache and a whole lot of tricky RewriteRules.

    There’s some other cleverness here in 2.3. If all approaches fail, it now tries to do some intelligent handling of the string and figure out what you were trying to get to. Say you have a URL that looks like this:
    http://example.com/misc/my-post

    If that was excessively long, it might get truncated when you post on forums and such, and end up being this:
    http://example.com/misc/my-po

    So since that doesn’t match anything, it matches what it can and then looks for matches using my-po* to see if it can find something. When it does, it sends a 301 redirect to force the browser to go to the right place. This helps with search engines getting the right links from bad posts and such. There’s some other things it can do along those same lines too.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘How permalinks work?’ is closed to new replies.